我有3个RadioButton
控件,它们是在我定义的自定义类和ItemsControl
的帮助下动态添加到WPF
程序中的。我的自定义类很简单;它有两个属性,一个是标识符,另一个是表示Checked/Unchecked
状态的bool
。
public class RadioButtonItem
{
public string ItemName { get; set; }
public bool IsChecked { get; set; }
}
我使用的是MVVMLight
,ViewModel
和DataContext
等都是正确设置的。
在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>
请注意,i
是Interactivity
名称空间: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>
发布于 2019-09-06 01:56:44
在ItemsControl
内部,绑定到RadioButtonCheckedCommand试图根据item - RadioButtonItem
实例来解决这个问题
但是该命令是在MainViewModel中定义的。它在StackPanel中工作,因为没有绑定到RadioButtonItems。
解决方案是绑定到ItemsControl.DataContext (与Grid.DataContext相同):
<i:InvokeCommandAction Command="{Binding Path=DataContext.RadioButtonCheckedCommand, ElementName=LayoutRoot}"/>
发布于 2019-09-06 02:24:20
前面的答案是完美的。如果你不想使用元素名称"LayoutRoot“,这只是另一种解决方案。
<i:InvokeCommandAction Command="{Binding Path=DataContext.RadioButtonCheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/>
https://stackoverflow.com/questions/57810719
复制相似问题