2009-11-27 37 views
1

我的自定義用戶控件的依賴屬性綁定正確,如果該值靜態在XAML定義調用它,就像這樣:爲什麼我的Custom UserControl的依賴屬性不能與綁定一起使用?

ItemTypeIdCode="addresses" 

但如果該值綁定動態本身:

ItemTypeIdCode="{Binding ItemTypeIdCode}" 

我必須對我的自定義UserControl做些什麼,以便它的依賴項屬性對自身綁定的值做出反應另一種控制?

這裏是我的代碼:

XAML:

<Window x:Class="TestDepenProp.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls="clr-namespace:TestDepenProp.Controls" 
    Title="Window1" Height="300" Width="300"> 
    <StackPanel 
     HorizontalAlignment="Left" 
     Margin="10"> 
     <controls:DropDown 
      ItemTypeIdCode="{Binding ItemTypeIdCode}" 
      SelectedValue="672" 
      Width="150" 
      Margin="0 0 0 5"/> 
     <TextBlock Text="{Binding ItemTypeIdCode}"/> 
    </StackPanel> 
</Window> 

代碼隱藏:

using System.Windows; 
using System.ComponentModel; 

namespace TestDepenProp 
{ 
    public partial class Window1 : Window, INotifyPropertyChanged 
    { 
     #region ViewModelProperty: ItemTypeIdCode 
     private string _itemTypeIdCode; 
     public string ItemTypeIdCode 
     { 
      get 
      { 
       return _itemTypeIdCode; 
      } 

      set 
      { 
       _itemTypeIdCode = value; 
       OnPropertyChanged("ItemTypeIdCode"); 
      } 
     } 
     #endregion 

     public Window1() 
     { 
      InitializeComponent(); 
      DataContext = this; 

      ItemTypeIdCode = "addresses"; 
     } 
     #region INotifiedProperty Block 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     #endregion 
    } 
} 

DropDown.xaml:

<UserControl x:Class="TestDepenProp.Controls.DropDown" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel> 
     <ComboBox SelectedValuePath="Key" 
        DisplayMemberPath="Value" 
        SelectedValue="{Binding SelectedValue}" 
        Margin="0 0 0 10" 
        ItemsSource="{Binding DropDownValues}" /> 
    </StackPanel> 
</UserControl> 

DropDown.xaml.cs:

using System.Windows; 
using System.Windows.Controls; 
using System.ComponentModel; 
using System.Collections.ObjectModel; 
using System.Collections.Generic; 

namespace TestDepenProp.Controls 
{ 
    public partial class DropDown : UserControl, INotifyPropertyChanged 
    { 

     public static readonly DependencyProperty ItemTypeIdCodeProperty = DependencyProperty.Register("ItemTypeIdCode", typeof(string), typeof(DropDown)); 

     public string ItemTypeIdCode 
     { 
      get { return (string)GetValue(ItemTypeIdCodeProperty); } 
      set { SetValue(ItemTypeIdCodeProperty, value); } 
     } 

     #region ViewModelProperty: DropDownValues 
     private ObservableCollection<KeyValuePair<string, string>> _dropDownValues = new ObservableCollection<KeyValuePair<string, string>>(); 
     public ObservableCollection<KeyValuePair<string, string>> DropDownValues 
     { 
      get 
      { 
       return _dropDownValues; 
      } 

      set 
      { 
       _dropDownValues = value; 
       OnPropertyChanged("DropDownValues"); 
      } 
     } 
     #endregion 

     #region ViewModelProperty: SelectedValue 
     private string _selectedValue; 
     public string SelectedValue 
     { 
      get 
      { 
       return _selectedValue; 
      } 

      set 
      { 
       _selectedValue = value; 
       OnPropertyChanged("SelectedValue"); 
      } 
     } 
     #endregion 

     public DropDown() 
     { 
      InitializeComponent(); 
      DataContext = this; 

      Loaded += new RoutedEventHandler(DropDown_Loaded); 
     } 

     void DropDown_Loaded(object sender, RoutedEventArgs e) 
     { 
      GetDropDownValues(); 
     } 

     void GetDropDownValues() 
     { 
      switch (ItemTypeIdCode) 
      { 
       case "addresses": 
        DropDownValues.Add(new KeyValuePair<string, string>("111", "762 Main St.")); 
        DropDownValues.Add(new KeyValuePair<string, string>("222", "7384 First Ave.")); 
        DropDownValues.Add(new KeyValuePair<string, string>("333", "8728 Second St.")); 
        break; 
       case "customers": 
        DropDownValues.Add(new KeyValuePair<string, string>("672", "Jim Smith")); 
        DropDownValues.Add(new KeyValuePair<string, string>("281", "James Anders")); 
        DropDownValues.Add(new KeyValuePair<string, string>("321", "Angie Wonderson")); 
        DropDownValues.Add(new KeyValuePair<string, string>("221", "Hal Cloud")); 
        DropDownValues.Add(new KeyValuePair<string, string>("123", "Hugh Brandley")); 
        break; 
       default: 
        break; 
      } 
     } 


     #region INotifiedProperty Block 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 

      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     #endregion 


    } 
} 

回答

1

我想你需要PropertyChangedCallback添加到您的ItemTypeIdCodeProperty依賴屬性。在這個回調中你要調用GetDropDownValues()。

0

我的猜測是Combobox沒有顯示任何條目。這確實有點棘手。一個控件在2個完全獨立的「地址空間」中工作。首先是它自己的數據,像所有的依賴屬性。第二個「地址空間」是它從它的父項繼承的數據上下文。如果沒有特別的做法,所有的綁定都可以在數據上下文中工作。如果你想引用控件的數據,你需要一個明確的「參考」添加到你自己:是不是需要

<UserControl x:Class="TestDepenProp.Controls.DropDown" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Name="self"> 
<StackPanel> 
    <ComboBox SelectedValuePath="Key" 
       DisplayMemberPath="Value" 
       SelectedValue="{Binding SelectedValue, ElementName=self}" 
       Margin="0 0 0 10" 
       ItemsSource="{Binding DropDownValues, ElementName=self}" /> 
</StackPanel> 

OnPropertyChanged,這已經由依賴項屬性的邏輯處理。

相關問題