动态创建控件时,单选按钮上的已检查事件命令绑定不起作用



我有 3 个RadioButton控件,在我定义的自定义类和ItemsControl的帮助下,我可以动态地添加到我的WPF程序中。我的自定义类很简单;它有两个属性,一个是标识符,另一个是表示Checked/Unchecked状态的bool

public class RadioButtonItem
{
public string ItemName { get; set; }
public bool IsChecked { get; set; }
}

我正在使用MVVMLight,以及ViewModelDataContext等都设置正确。

MainWindow中,我添加了RadioButton控件,如下所示:

<Grid x:Name="LayoutRoot">
<ItemsControl ItemsSource="{Binding Path=RadioButtons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding ItemName}" 
IsChecked="{Binding IsChecked}"
Margin="12" GroupName="ABC">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding RadioButtonCheckedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>

请注意,iInteractivity命名空间:xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

现在在我的ViewModel中,我创建了一个RadioButtonItem实例的列表,并在加载时XAML端创建了 3 个RadioButton控件,正如我所期望的那样。然后使用MVVMLight命令RelayCommand,我将Checked事件挂接到RadioButtonCheckedCommand命令,而命令又应该调用RadioButtonChecked()私有方法。

public class MainViewModel : ViewModelBase
{
private List<RadioButtonItem> _radioButtons;
public List<RadioButtonItem> RadioButtons
{
get => _radioButtons;
set { _radioButtons = value; RaisePropertyChanged(); }
}
public RelayCommand RadioButtonCheckedCommand { get; private set; }
public MainViewModel()
{
RadioButtons = new List<RadioButtonItem>
{
new RadioButtonItem() { ItemName = "A", IsChecked = false },
new RadioButtonItem() { ItemName = "B", IsChecked = false },
new RadioButtonItem() { ItemName = "C", IsChecked = false },
};
RadioButtonCheckedCommand = new RelayCommand(RadioButtonChecked);
}
private void RadioButtonChecked()
{
}
}

但是,当我运行程序时,它不会调用所述事件。它也没有给我任何错误。如何让事件在这样的动态创建的控件中工作?

为了验证我的命令绑定等是否有效,我还在窗口上创建了三个静态RadioButton控件,如下所示,并将它们附加到同一命令,它们在Checked事件上工作正常。

<StackPanel Grid.Row="1" HorizontalAlignment="Center">
<RadioButton Content="X" Margin="12">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding RadioButtonCheckedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Content="Y" Margin="12">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding RadioButtonCheckedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Content="Z" Margin="12">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding RadioButtonCheckedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</StackPanel>

之前的答案是完美的。如果您不想使用名称"LayoutRoot",这只是另一种解决方案。

<i:InvokeCommandAction Command="{Binding Path=DataContext.RadioButtonCheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/>

ItemsControl中,绑定到 RadioButtonCheckedCommand 正在尝试针对一个项目 -RadioButtonItem实例解析它

但它在 StackPanel 中工作,因为没有绑定到 RadioButtonItems。

解决方案是绑定到 ItemsControl.DataContext(与 Grid.DataContext 相同(:

<i:InvokeCommandAction Command="{Binding Path=DataContext.RadioButtonCheckedCommand, ElementName=LayoutRoot}"/>