什么类型的物业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>
1)请不要在标题中添加标签。 2)寻找绑定错误,他们可能会给你一个线索,知道发生了什么。你可以在Visual Studio的Debug输出窗口中找到它们。另一种解决方案是使用优秀的Snoop来浏览应用程序的可视化树并找出绑定错误。 – 2011-05-06 07:46:49