什麼類型的物業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