gpt4 book ai didi

c# - 如何通过绑定(bind)到 ViewModel 来设置正确的 RadioButton.IsChecked 属性为真?

转载 作者:行者123 更新时间:2023-12-02 17:52:39 24 4
gpt4 key购买 nike

我有以下场景,其中有这样一个类:

public class PetOwnerViewModel{
public PetOwnerStatus Status{get{return _petOwner.Status;}}

public ICommand SetStatusCommand {get{...}}
}

DataContext 是一组类似这样的 RadioButtons:

<Parent DataContext="{Binding Path=PetOwner}" >
<Parent.Resources>
<myenums:PetOwnerStatus x:Key="CATLOVER">
CatLover
</myenums:PetOwnerStatus>
<myenums:PetOwnerStatus x:Key="DOGLOVER">
DogLover
</myenums:PetOwnerStatus>
</Parent.Resources>
<StackPanel>
<RadioButton Name="catLoverRadioButton"
Command="{Binding SetStatusCommand}"
CommandParameter="{StaticResource DOGLOVER}"
GroupName="PetOwnerStatusRadioButtonGroup">
Cat Lover
</RadioButton>
<RadioButton Name="dogLoverRadioButton"
Command="{Binding SetStatusCommand}"
CommandParameter="{StaticResource CATLOVER}"
GroupName="SubjectStatusRadioButtonGroup" >
Dog Lover
</RadioButton>
</StackPanel>
</Parent>

如何将 View 绑定(bind)到 ViewModel,以便在 PetOwnerViewModel.Status 返回 PetOwnerStatus.CatLover 时,catLoverRadioButton.IsChecked 为真。

最佳答案

您可以使用数据模板使这类事情变得非常动态,例如

(-- 编辑: 使用已经具有 SelectedItem 属性的 ListBox 更有意义,请参阅this revised answer --)

public partial class MainWindow : Window, INotifyPropertyChanged
{
//For simplicity in put everything in the Window rather than models and view-models
public enum TestEnum { Ichi, Ni, San }

private TestEnum _EnumValue;
public TestEnum EnumValue
{
get { return _EnumValue; }
set
{
if (_EnumValue != value)
{
_EnumValue = value;
PropertyChanged.Notify(() => this.EnumValue);
}
}
}

//...
}
<ItemsControl>
<ItemsControl.Resources>
<!-- Gets the enum values -->
<ObjectDataProvider x:Key="items" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:MainWindow+TestEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<!-- A MultiValueConverter which compares for equality -->
<vc:EqualityComparisonConverter x:Key="eqc" />
</ItemsControl.Resources>
<ItemsControl.ItemsSource>
<Binding Source="{StaticResource items}" />
</ItemsControl.ItemsSource>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding}" GroupName="TestEnumGroup"
Command="{x:Static local:Commands.DoStuff}" CommandParameter="{Binding}">
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource eqc}" Mode="OneWay">
<!-- This should point to the viewmodel enum property -->
<Binding ElementName="Window" Path="DataContext.EnumValue" />
<!-- This passes the DataContext, the enum value behind the templated item, to the converter -->
<Binding />
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
private void DoStuff_Executed(object sender, ExecutedRoutedEventArgs e)
{
TestEnum enumval = (TestEnum)e.Parameter;
EnumValue = enumval;
}

这对原始枚举值进行操作,您可以使用属性通过显示友好的字符串来扩充它们。

因为 IsChecked 绑定(bind)在所有 RadioButton 上,所以 RadioButton.GroupName 变得多余。

(我没有提供我对 EqualityComparisonConverter 的实现,因为它可能很糟糕,不过正确实现它应该不会太难)

关于c# - 如何通过绑定(bind)到 ViewModel 来设置正确的 RadioButton.IsChecked 属性为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6258505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com