【发布时间】:2019-04-11 01:57:11
【问题描述】:
我有一个不同类型的对象列表。其中一些是RadioProperty 类型。
每个对象都有一些属性。感兴趣的如下:
string PropName; // property name
string Value; // current property value
Dictionary<string, string> Values; // possible values - name, value
Value 应随时具有字典中可用的值之一。
我想要做的是以某种方式将它绑定到一个单选按钮组,这样我就可以从字典中的可用值中选择属性的值。
目前这就是我所拥有的:
为了演示目的,我已经简化了代码。希望我没有错过任何重要的事情。
XAML
<StackPanel>
<StackPanel.Resources>
<local:RadioPropertyConverter x:Key="radioPropertyConverter" />
</StackPanel.Resources>
<ItemsControl x:Name="PropertyList" ItemsSource="{Binding PropList}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type proptool:RadioProperty}">
<StackPanel>
<Grid>
<StackPanel>
<ItemsControl ItemsSource="{Binding Values}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Path=Key}">
<!-- BEGIN: GroupName is equal to PropName -->
<RadioButton.GroupName>
<Binding Path="DataContext.PropName">
<Binding.RelativeSource>
<RelativeSource Mode="FindAncestor"
AncestorType="{x:Type TypeName=StackPanel}" />
</Binding.RelativeSource>
</Binding>
</RadioButton.GroupName>
<!-- END -->
<RadioButton.IsChecked>
<!-- specifying only `Value` for Path will fail at
runtime with the following error:
System.Windows.Markup.XamlParseException: 'A TwoWay or
OneWayToSource binding cannot work on the read-only
property 'Value' of type
'System.Collections.Generic.KeyValuePair`2[System.String,System.String]'.' -->
<Binding Converter="{StaticResource radioPropertyConverter}"
Path="/Value" ConverterParameter="Y" />
</RadioButton.IsChecked>
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</StackPanel>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</StackPanel>
PropList 的类型为 List<RadioProperty>。
ConverterParameter 的值应等于字典中的当前值。似乎无法为 ConverterParameter 指定绑定:
System.Windows.Markup.XamlParseException:“无法设置“绑定” “Binding”类型的“ConverterParameter”属性。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。'
C#
public class RadioPropertyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value?.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return value?.Equals(true) == true ? parameter : Binding.DoNothing;
}
}
我不认为转换器应该是这样的,但我目前遇到的问题是从未调用过 Convert/ConvertBack 方法。
我想要实现的是拥有一个属性列表,每个属性在不同的控件中都有一些细节。每个属性可能都有自己的列表 (Values),它应该作为单选按钮列表呈现给用户。根据Value 的值,应该选择一个特定的单选按钮。如果选择了不同的单选按钮,则该值应相应更改。
所以我认为应该使用Value 完成绑定,并根据字典的内容呈现单选按钮(我已经有了这个工作)。
我恳请您不要向我指出其他 SO 问题,因为我非常有信心我已经完成了与我的问题相关的大部分问题。当然,除非你理解了我的问题,并且你认为我可能遗漏了一些已经回答的问题中的一些细节。
对于我已经经历过的特定场景,一些可能更相关的 SO 问题:
【问题讨论】:
-
您将路径指定为
Path="/Value"而不是Path="Value"的任何原因? -
@RandRandom,我在 XAML cmets 中
RadioButton.IsChecked属性元素之后提到了原因。我仍然不知道为什么我只是指定Value它不起作用。 -
我后来在我的评论中看到了这一点 - 但你确实意识到你所做的唯一一件事就是破坏你的有效输入,如果你写
Path="WTF_IsWrong"现在你的运行时错误关于 wpf 抱怨两个由于您不再绑定到只读的Value属性,因此方式 binging 也消失了。 - 所以,是的,你的错误消失了,只是因为你破坏了你的有效输入。 -
如果我为 Binding 的 Path 属性指定
Value但它与 MultiBinding 一起使用,我仍然不明白为什么它不起作用。我会试着用谷歌搜索一下,看看有没有发现……
标签: c# wpf data-binding radio-button