首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态创建控件时,RadioButton上的已检查事件命令绑定不起作用

动态创建控件时,RadioButton上的已检查事件命令绑定不起作用
EN

Stack Overflow用户
提问于 2019-09-06 01:50:38
回答 2查看 676关注 0票数 0

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

代码语言:javascript
运行
复制
public class RadioButtonItem
{
    public string ItemName { get; set; }
    public bool IsChecked { get; set; }
}

我使用的是MVVMLightViewModelDataContext等都是正确设置的。

MainWindow中,我像这样添加了RadioButton控件:

代码语言:javascript
运行
复制
<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()私有方法。

代码语言:javascript
运行
复制
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事件上工作得很好。

代码语言:javascript
运行
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-06 01:56:44

ItemsControl内部,绑定到RadioButtonCheckedCommand试图根据item - RadioButtonItem实例来解决这个问题

但是该命令是在MainViewModel中定义的。它在StackPanel中工作,因为没有绑定到RadioButtonItems。

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

代码语言:javascript
运行
复制
<i:InvokeCommandAction Command="{Binding Path=DataContext.RadioButtonCheckedCommand, ElementName=LayoutRoot}"/>
票数 1
EN

Stack Overflow用户

发布于 2019-09-06 02:24:20

前面的答案是完美的。如果你不想使用元素名称"LayoutRoot“,这只是另一种解决方案。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57810719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档