好吧,我剛剛發佈this的問題,但我想,我沒有做錯什麼。我的代碼(和所有答案的代碼)是正確的,但我的dev-maschine運行在.NET4.5上,這顯然與組合框的綁定有問題...雙向綁定組合框的靜態屬性在.NET 4.5
因此,這裏有一個新版本問題:如何將組合框的SelectedItem雙向綁定到.NET4.5中的靜態屬性?
以下代碼片段在.net4中工作,但不在.NET4.5中。在4.5中,只是選定的值不會傳回到我的靜態屬性中。
我的視圖模型:
public class MainWindowViewModel
{
public static List<String> MyElements { get; set; }
public static string SelectedElement { get; set; }
static MainWindowViewModel()
{
MyElements = new List<string>() {"a", "b", "c"};
SelectedElement = "a";
}
}
我的XAML
<Window.Resources>
<me:MainWindowViewModel x:Key="model"/>
</Window.Resources>
<StackPanel>
<ComboBox
ItemsSource="{Binding Source={x:Static me:MainWindowViewModel.MyElements}, Mode=OneWay}"
SelectedItem="{Binding Source={StaticResource model}, Path=SelectedElement}" />
</StackPanel>
有誰有一個想法如何實現這種雙向在.NET4.5靜態屬性組合框的的SelectedItem的結合?