0
我有一個UWP應用程序,我將一個ComboBox綁定到一個字典。除了一個問題,這是有用的。當我嘗試在我的視圖模型中設置綁定的SelectedValue時,組合框重置爲空狀態。綁定ComboBox到UWP中的Enum字典
我試着在WPF中做同樣的事情,它沒有這個問題。在線看,我發現this頁面正在做我正在用WPF做的事情,但我在UWP上找不到任何東西。
當更新綁定值時,我需要做些什麼來使ComboBox不重置?
這是一個簡化的例子。我使用PropertyChanged.Fody和MvvmLightLibs
視圖模型:
[ImplementPropertyChanged]
public class ViewModel
{
public ICommand SetZeroCommand { get; set; }
public ICommand ShowValueCommand { get; set; }
public ViewModel()
{
SetZeroCommand = new RelayCommand(SetZero);
ShowValueCommand = new RelayCommand(ShowValue);
}
public Numbers Selected { get; set; } = Numbers.One;
public Dictionary<Numbers, string> Dict { get; } = new Dictionary<Numbers, string>()
{
[Numbers.Zero] = "Zero",
[Numbers.One] = "One",
[Numbers.Two] = "Two"
};
private async void ShowValue()
{
var dialog = new MessageDialog(Selected.ToString());
await dialog.ShowAsync();
}
private void SetZero()
{
Selected = Numbers.Zero;
}
public enum Numbers
{
Zero,
One,
Two
}
}
的XAML:
<Page
x:Class="UwpBinding.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpBinding"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
DataContext="{Binding MainWindow, Source={StaticResource Locator}}">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ComboBox Margin="105,163,0,0" ItemsSource="{Binding Dict}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Selected, Mode=TwoWay}"/>
<Button Content="Show" Command="{Binding ShowValueCommand}" Margin="25,304,0,304"/>
<Button Content="Set to 0" Command="{Binding SetZeroCommand}" Margin="10,373,0,235"/>
</Grid>
</Page>
你有沒有登錄反對這種錯誤? SelectedValuePath如果僅適用於整數,那麼它就毫無意義。 : - /我知道。如果你沒有,我會的。謝謝。 –