0

我有一個窗口上的「佈局」和「SelectedLocation」DependencyProperty的ObservableCollection。 SelectedLocation有一個名爲「Layout」的屬性,它是一個包含「Name」等字段的對象。我試圖將組合框綁定到SelectedLayout,但它不起作用。 以下不起作用,我嘗試綁定到SelectedItem而不是無用的。我相信這可能是一些做與我結合SelectedLocation的DependencyProperty的子屬性(雖然這並不執行INotifyPropertyChanged的事實。WPF:嵌套依賴屬性

<ComboBox Grid.Row="2" Grid.Column="0" x:Name="cboLayout" ItemsSource="{Binding Layouts,ElementName=root}" SelectedValue="{Binding SelectedLocation.Layout.LayoutID,ElementName=root}" DisplayMemberPath="{Binding Name}" SelectedValuePath="LayoutID" /> 

但是,下面的工程(也勢必「SelectedLocation」 DP:

<TextBox Grid.Row="4" Grid.Column="1" x:Name="txtName" Text="{Binding SelectedLocation.Name,ElementName=root,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /> 
+0

1)請不要在標題中添加標籤。 2)尋找綁定錯誤,他們可能會給你一個線索,知道發生了什麼。你可以在Visual Studio的Debug輸出窗口中找到它們。另一種解決方案是使用優秀的Snoop來瀏覽應用程序的可視化樹並找出綁定錯誤。 – 2011-05-06 07:46:49

回答

1

什麼類型的物業Layouts有我想是這樣的:?IEnumerable<Layout> 但是你選擇的值綁定到Layout.LayoutID所以你得到的情況,當組合框包含Layout對象和你。嘗試通過Int標識符來選擇它。當然綁定引擎在那裏找不到任何Int

我不知道你的代碼的細節,所以我可以提出一件事:儘量減少你的綁定表達式:SelectedItem="{Binding SelectedLocation.Layout,ElementName=root}

如果沒有成功,請提供更多代碼以幫助我瞭解正在發生的事情。

==== ==== UPDATE

正如我已經說過了,你顯然是做錯了什麼。但我不是超自然主義者,無法猜測你失敗的原因(沒有你的代碼)。如果你不想分享你的代碼,我決定提供一個簡單的例子來證明一切正常。看看下面顯示的代碼,告訴我你的應用程序有什麼不同。

客艙佈局暴露財產LayoutId:

public class Layout 
{ 
    public Layout(string id) 
    { 
     this.LayoutId = id; 
    } 

    public string LayoutId 
    { 
     get; 
     private set; 
    } 

    public override string ToString() 
    { 
     return string.Format("layout #{0}", this.LayoutId); 
    } 
} 

類SelectionLocation已嵌套屬性的佈局:

public class SelectedLocation : INotifyPropertyChanged 
{ 
    private Layout _layout; 

    public Layout Layout 
    { 
     get 
     { 
      return this._layout; 
     } 
     set 
     { 
      this._layout = value; 
      this.OnPropertyChanged("Layout"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string name) 
    { 
     var safeEvent = this.PropertyChanged; 
     if (safeEvent != null) 
     { 
      safeEvent(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 

門窗類依賴屬性(實際上,在我的例子StartupView是用戶控件,但沒關係):

public partial class StartupView : UserControl 
{ 
    public StartupView() 
    { 
     InitializeComponent(); 

     this.Layouts = new Layout[] { new Layout("AAA"), new Layout("BBB"), new Layout("CCC") }; 
     this.SelectedLocation = new SelectedLocation(); 
     this.SelectedLocation.Layout = this.Layouts.ElementAt(1); 
    } 

    public IEnumerable<Layout> Layouts 
    { 
     get 
     { 
      return (IEnumerable<Layout>)this.GetValue(StartupView.LayoutsProperty); 
     } 
     set 
     { 
      this.SetValue(StartupView.LayoutsProperty, value); 
     } 
    } 

    public static readonly DependencyProperty LayoutsProperty = 
     DependencyProperty.Register("Layouts", 
      typeof(IEnumerable<Layout>), 
      typeof(StartupView), 
      new FrameworkPropertyMetadata(null)); 

    public SelectedLocation SelectedLocation 
    { 
     get 
     { 
      return (SelectedLocation)this.GetValue(StartupView.SelectedLocationProperty); 
     } 
     set 
     { 
      this.SetValue(StartupView.SelectedLocationProperty, value); 
     } 
    } 

    public static readonly DependencyProperty SelectedLocationProperty = 
     DependencyProperty.Register("SelectedLocation", 
      typeof(SelectedLocation), 
      typeof(StartupView), 
      new FrameworkPropertyMetadata(null)); 
} 

StartupView的XAML:

<UserControl x:Class="Test.StartupView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:self="clr-namespace:HandyCopy" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Name="Root"> 
    <WrapPanel> 
     <ComboBox ItemsSource="{Binding Path=Layouts,ElementName=Root}" 
        SelectedItem="{Binding Path=SelectedLocation.Layout, ElementName=Root}"/> 
    </WrapPanel> 
</UserControl> 
+0

我試過了。 LayoutID是一個字符串屬性,它是唯一的ID。 「佈局」屬性是一個「佈局」對象的ObservableCollection,每個對象都有一個「佈局ID」屬性。 – Echilon 2011-05-06 08:13:21

+0

沒關係。你犯了明顯的錯誤:當你的項目是佈局時,不可能通過字符串選擇值。另一種奇怪的是使用SelectedValue而不是SelectedItem。試試最後一個,讓我知道。 – 2011-05-06 09:04:16

+0

我試過SelectedValue和SelectedItem,有和沒有SelectedValuePath,但似乎沒有任何工作。 – Echilon 2011-05-06 10:09:33