[英]Binding ComboBox to Enum Dictionary in UWP
I have a UWP app where I'm binding a ComboBox to a Dictionary. 我有一个UWP应用,将ComboBox绑定到Dictionary。 This is sort of working except for one issue. 除了一个问题,这是一种工作。 When I try to set the bound SelectedValue in my view model, the ComboBox resets to a null state. 当我尝试在视图模型中设置绑定的SelectedValue时,ComboBox重置为空状态。
I tried doing the exact same thing in WPF, and it doesn't have this issue. 我尝试在WPF中做完全相同的事情,但没有这个问题。 Looking online, I found this page doing exactly what I'm doing with WPF, but I couldn't find anything on UWP. 在网上查看时,我发现此页面与WPF完全相同,但是在UWP上找不到任何内容。
What do I need to do to make the ComboBox not reset when updating the bound value? 更新绑定值时,我需要怎么做才能不重置ComboBox?
Here's a simplified example. 这是一个简化的示例。 I'm using PropertyChanged.Fody and MvvmLightLibs 我正在使用PropertyChanged.Fody和MvvmLightLibs
View Model: 查看模型:
[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: 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>
I made a basic demo and reproduced your problem. 我做了一个基本的演示,并重现了您的问题。 After researching, I found the problem: Combox.SelectedValue
doesn't work with Enumeration
. 经过研究,我发现了问题: Combox.SelectedValue
不适用于Enumeration
。
Current workaround is to use SelectedIndex
instead. 当前的解决方法是改用SelectedIndex
。
For example: in your ViewModel change the codes like below: 例如:在您的ViewModel中,更改如下代码:
public int Selected { get; set; } = 1;
...
private void SetZero()
{
Selected = 0;
}
...
private async void ShowValue()
{
Numbers tmp=Numbers.Zero;
switch (Selected)
{
case 0: tmp = Numbers.Zero;
break;
case 1:tmp = Numbers.One;
break;
case 2:tmp = Numbers.Two;
break;
}
var dialog = new MessageDialog(tmp.ToString());
await dialog.ShowAsync();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.