gpt4 book ai didi

ItemsControl 上的 WPF MVVM 单选按钮

转载 作者:行者123 更新时间:2023-12-03 11:35:22 26 4
gpt4 key购买 nike

我之前已经将枚举绑定(bind)到单选按钮,并且我大致了解它是如何工作的。我使用了这个问题的替代实现:How to bind RadioButtons to an enum?

我想生成一个自定义类型的运行时枚举集,而不是枚举,并将它们呈现为一组单选按钮。我得到了一个 View ,它使用 ListView 处理运行时枚举集。 , 绑定(bind)到 ItemsSourceSelectedItem属性,所以我的ViewModel已正确连接。现在我正在尝试从 ListView 切换到 ItemsControl带有单选按钮。

据我所知,这是:

<Window.Resources>
<vm:InstanceToBooleanConverter x:Key="InstanceToBooleanConverter" />
</Window.Resources>

<!-- ... -->

<ItemsControl ItemsSource="{Binding ItemSelections}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type vm:ISomeType}">
<RadioButton Content="{Binding Name}"
IsChecked="{Binding Path=SelectedItem, Converter={StaticResource InstanceToBooleanConverter}, ConverterParameter={Binding}}"
Grid.Column="0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
InstanceToBooleanConverterEnumToBooleanConverter 具有相同的实现从另一个问题。这似乎是正确的,因为它似乎只是调用了 Equals方法:
public class InstanceToBooleanConverter : 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) ? parameter : Binding.DoNothing;
}
}

我现在遇到的问题是我无法弄清楚如何将运行时值作为 ConverterParameter 发送。 .当我尝试(使用上面的代码)时,我收到此错误:

A 'Binding' cannot be set on the 'ConverterParameter' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.



有没有办法绑定(bind)到项目实例,并将其传递给 IValueConverter ?

最佳答案

现在我知道 x:Shared (感谢您的 other question ),我放弃我之前的回答并说 MultiBinding毕竟是要走的路。

XAML:

<StackPanel>
<TextBlock Text="{Binding SelectedChoice}" />

<ItemsControl ItemsSource="{Binding Choices}">
<ItemsControl.Resources>
<local:MyConverter x:Key="myConverter" x:Shared="false" />
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton>
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource myConverter}" >
<Binding Path="DataContext.SelectedChoice" RelativeSource="{RelativeSource AncestorType=UserControl}" />
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=Self}" />
</MultiBinding>
</RadioButton.IsChecked>
<TextBlock Text="{Binding}" />
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>

View 模型:
class Viewmodel : INPC
{
public Viewmodel()
{
Choices = new List<string>() { "one", "two", "three" };
SelectedChoice = Choices[0];
}

public List<string> Choices { get; set; }

string selectedChoice;
public string SelectedChoice
{
get { return selectedChoice; }
set
{
if (selectedChoice != value)
{
selectedChoice = value;
OnPropertyChanged("SelectedChoice");
}
}
}
}

转换器:
public class MyConverter : IMultiValueConverter
{
object selectedValue;
object myValue;

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
selectedValue = values[0];
myValue = values[1];

return selectedValue == myValue;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
{
return new object[] { myValue, Binding.DoNothing };
}
else
{
return new object[] { Binding.DoNothing, Binding.DoNothing };
}

}
}

关于ItemsControl 上的 WPF MVVM 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5891924/

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