C# 带有一组单选按钮的WPF ListView,并选择默认值

C# 带有一组单选按钮的WPF ListView,并选择默认值,c#,wpf,xaml,wpf-controls,C#,Wpf,Xaml,Wpf Controls,今天,我对选中的默认复选框有问题。但首先我要展示我的代码: <ScrollViewer> <ListView ItemsSource="{Binding itemsSource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> <ListView.ItemTemplate> <DataTemplate> <E

今天,我对选中的默认
复选框有问题。但首先我要展示我的代码:

<ScrollViewer>
    <ListView ItemsSource="{Binding itemsSource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Expander IsExpanded="True">
                    <Expander.Header>
                        <Label Content="{Binding AttrName, Mode=OneWay}" />
                    </Expander.Header>

                    <ListView Margin="20, 0, 0, 0" ItemsSource="{Binding subItemSource}" BorderBrush="Transparent" >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <RadioButton GroupName="{Binding DataContext.AttrName, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                                    Content="{Binding}"
                                                    <!-- What should I bind to to get item checked? -->
                                                    IsChecked={}/>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

                </Expander>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ScrollViewer>

现在我只想在实际绑定值等于
DefaultValue
时标记
RadioButton
。我该怎么做?我应该编写验证器吗?

我将从编写一个转换器类开始

class ElementComparer : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values[0] == values[1];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后将转换器声明为一个资源,其中l:是您的命名空间到转换器

<l:ElementComparer x:Key="ElementComparer"/>

然后在数据模板中

<DataTemplate>
    <RadioButton GroupName="{Binding DataContext.AttrName, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                 Content="{Binding}"
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource ElementComparer}" Mode="OneWay">
                <Binding Path="DataContext.DefaultValue" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
                <Binding />
            </MultiBinding>
        </RadioButton.IsChecked>


我将从编写一个转换器类开始

class ElementComparer : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values[0] == values[1];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后将转换器声明为一个资源,其中l:是您的命名空间到转换器

<l:ElementComparer x:Key="ElementComparer"/>

然后在数据模板中

<DataTemplate>
    <RadioButton GroupName="{Binding DataContext.AttrName, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                 Content="{Binding}"
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource ElementComparer}" Mode="OneWay">
                <Binding Path="DataContext.DefaultValue" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
                <Binding />
            </MultiBinding>
        </RadioButton.IsChecked>


我将从编写一个转换器类开始

class ElementComparer : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values[0] == values[1];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后将转换器声明为一个资源,其中l:是您的命名空间到转换器

<l:ElementComparer x:Key="ElementComparer"/>

然后在数据模板中

<DataTemplate>
    <RadioButton GroupName="{Binding DataContext.AttrName, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                 Content="{Binding}"
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource ElementComparer}" Mode="OneWay">
                <Binding Path="DataContext.DefaultValue" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
                <Binding />
            </MultiBinding>
        </RadioButton.IsChecked>


我将从编写一个转换器类开始

class ElementComparer : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values[0] == values[1];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后将转换器声明为一个资源,其中l:是您的命名空间到转换器

<l:ElementComparer x:Key="ElementComparer"/>

然后在数据模板中

<DataTemplate>
    <RadioButton GroupName="{Binding DataContext.AttrName, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                 Content="{Binding}"
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource ElementComparer}" Mode="OneWay">
                <Binding Path="DataContext.DefaultValue" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
                <Binding />
            </MultiBinding>
        </RadioButton.IsChecked>


很好的解决方案。我没有想到在这里使用
MultiBinding
。回答得很好。非常感谢。另一个问题-如何实现ConvertBack?我试图使其成为双向绑定,但应用程序崩溃,并显示消息“双向绑定需要路径或xpath”。对于双向绑定,您无法将
与当前项绑定,而是必须绑定数据项中的某些属性。因为您的项目源是int,所以在这种情况下不太可能,所以您需要重新设计您的数据项,然后才能使用convert back for bindingsGreat解决方案。我没有想到在这里使用
MultiBinding
。回答得很好。非常感谢。另一个问题-如何实现ConvertBack?我试图使其成为双向绑定,但应用程序崩溃,并显示消息“双向绑定需要路径或xpath”。对于双向绑定,您无法将
与当前项绑定,而是必须绑定数据项中的某些属性。因为您的项目源是int,所以在这种情况下不太可能,所以您需要重新设计您的数据项,然后才能使用convert back for bindingsGreat解决方案。我没有想到在这里使用
MultiBinding
。回答得很好。非常感谢。另一个问题-如何实现ConvertBack?我试图使其成为双向绑定,但应用程序崩溃,并显示消息“双向绑定需要路径或xpath”。对于双向绑定,您无法将
与当前项绑定,而是必须绑定数据项中的某些属性。因为您的项目源是int,所以在这种情况下不太可能,所以您需要重新设计您的数据项,然后才能使用convert back for bindingsGreat解决方案。我没有想到在这里使用
MultiBinding
。回答得很好。非常感谢。另一个问题-如何实现ConvertBack?我试图使其成为双向绑定,但应用程序崩溃,并显示消息“双向绑定需要路径或xpath”。对于双向绑定,您无法将
与当前项绑定,而是必须绑定数据项中的某些属性。因为您的项目源是int,所以在这种情况下不太可能,所以您需要重新设计您的数据项,然后才能使用convert-back进行绑定