如何将 Control 的内容属性作为 CommandParameter WPF 传递

问题描述 投票:0回答:2

我有一个 WPF 项目,有 19 个单选按钮。我想设置每个命令属性。 为了分隔所有命令操作,我想将 RadioButton Content 属性作为 CommandParameter 发送。

我尝试为 CommandParameter 属性定义 setter,并为 Value 设置

{Binding Path=Content}
,如下所示。

但这不行。

 <Style TargetType="RadioButton">
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Command" Value="{Binding SelectionChangedCommand}"/>
        <Setter Property="CommandParameter" Value="{Binding Path=Content}"/>
    </Style>
          

 <RadioButton Content="Area"/>
            <RadioButton Content="Unit"
                         Grid.Row="1"/>
            
            <RadioButton Content="Transom Count"
                         Grid.Row="2"
                         CommandParameter="Transom Count"/>
            <RadioButton Content="Mullion Count"
                         Grid.Row="3"/>
            <RadioButton Content="Transom And Mulliom Count"
                         Grid.Row="4"/>
            <RadioButton Content="Frame Count"
                         Grid.Row="5"/>
            <RadioButton Content="Frame + Transom + Mullion Count"
                         Grid.Row="6"/>
            <RadioButton Content="Corner Points"
                         Grid.Row="7"/>
            <RadioButton Content="T Connections"
                         Grid.Row="8"/>

            <RadioButton Content="Left"
                         Grid.Column="1"/>
            <RadioButton Content="Right"
                         Grid.Column="1"
                         Grid.Row="1"/>
            <RadioButton Content="Top"
                         Grid.Column="1"
                         Grid.Row="2"/>
            <RadioButton Content="Bottom"
                         Grid.Column="1"
                         Grid.Row="3"/>
            <RadioButton Content="Perimeter"
                         Grid.Column="1"
                         Grid.Row="4"/>
            <RadioButton Content="Transom Length"
                         Grid.Column="1"
                         Grid.Row="5"/>
            <RadioButton Content="Mullion Length"
                         Grid.Column="1"
                         Grid.Row="6"/>
            <RadioButton Content="Transom + Mullion Length"
                         Grid.Column="1"
                         Grid.Row="7"/>
            <RadioButton Content="Perimeter + Transom + Mullion Length"
                         Grid.Column="1"
                         Grid.Row="8"/>

c# wpf mvvm radio-button
2个回答
0
投票

在 WPF 中,您可以将控件的 Content 属性作为 CommandParameter 传递,方法是将其绑定到您正在使用的命令的 CommandParameter 属性。

以下是使用 Button 控件的示例:

在此示例中:

MyCommand 是您在 ViewModel 中定义的命令。 CommandParameter 设置为 Button 的 Content 属性。 relativeSource={RelativeSource Self} 指定您要绑定到当前控件(在本例中为 Button)的 Content 属性。 确保您的 ViewModel 具有接收此 CommandParameter 的属性。

请记住,CommandParameter 是您传递给 ViewModel 中的命令方法的附加参数。根据您正在使用的控件和 ViewModel 的结构相应地调整绑定。


0
投票
 <Style TargetType="RadioButton">
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Command" Value="{Binding SelectionChangedCommand}"/>
        <Setter Property="CommandParameter" Value="{Binding Content, RelativeSource={RelativeSource Self}}"/>
    </Style>
© www.soinside.com 2019 - 2024. All rights reserved.