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