Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取转换器中另一个控件的值_C#_Wpf_Binding - Fatal编程技术网

C# 获取转换器中另一个控件的值

C# 获取转换器中另一个控件的值,c#,wpf,binding,C#,Wpf,Binding,我有两个单选按钮,通过XPath绑定到XML。如果选择第一个单选按钮,则需要获取组合框的选定值,并将其设置为XML元素。但是,如果我选择第二个单选按钮,我只需要设置一个固定的硬编码值 当我选择第一个单选按钮时,从组合框中获取值时遇到问题。我尝试过使用ConverterParameter(我发现它不允许绑定),而使用多重绑定也没有帮助 请给我一些建议 谢谢 使用多绑定和多值转换器,您走上了正确的道路。下面是一个与您的需求类似的简单示例: <Window.Resources> &

我有两个单选按钮,通过XPath绑定到XML。如果选择第一个单选按钮,则需要获取组合框的选定值,并将其设置为XML元素。但是,如果我选择第二个单选按钮,我只需要设置一个固定的硬编码值

当我选择第一个单选按钮时,从组合框中获取值时遇到问题。我尝试过使用ConverterParameter(我发现它不允许绑定),而使用多重绑定也没有帮助

请给我一些建议


谢谢

使用多绑定和多值转换器,您走上了正确的道路。下面是一个与您的需求类似的简单示例:

<Window.Resources>
    <my:MyMultiValueConverter x:Key="MyMultiValueConverter" />
</Window.Resources>

<StackPanel Orientation="Vertical" >
    <RadioButton x:Name="FirstRadioButton" GroupName="MyButtonGroup">First</RadioButton>
    <RadioButton x:Name="SecondRadioButton" GroupName="MyButtonGroup">Second</RadioButton>
    <ComboBox x:Name="MyComboBox" SelectedValuePath="Content">
        <ComboBoxItem>First Item</ComboBoxItem>
        <ComboBoxItem>Second Item</ComboBoxItem>
    </ComboBox>

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
                <Binding ElementName="FirstRadioButton" Path="IsChecked" />
                <Binding ElementName="MyComboBox" Path="SelectedValue" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
</StackPanel>
编辑:

如果您只需要对RadioButton的选中事件作出反应,则可以执行以下操作:

<Window.DataContext>
    <my:MainWindowViewModel />
</Window.DataContext>

<StackPanel Orientation="Vertical">
    <RadioButton GroupName="MyButtonGroup" Content="First" IsChecked="True">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <i:InvokeCommandAction Command="{Binding PassSelectedValueToXmlCommand}" CommandParameter="{Binding SelectedValue, ElementName=MyComboBox}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </RadioButton>
    <RadioButton GroupName="MyButtonGroup" Content="Second">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <i:InvokeCommandAction Command="{Binding PassSelectedValueToXmlCommand}" CommandParameter="My static value" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </RadioButton>
    <ComboBox x:Name="MyComboBox" SelectedValuePath="Content">
        <ComboBoxItem Content="First item" IsSelected="True" />
        <ComboBoxItem Content="Second Item" />
    </ComboBox>
</StackPanel>

注意:您需要参考System.Windows.Interactivity并在窗口/页面/控件中使用它。。。xmlns:i=”http://schemas.microsoft.com/expression/2010/interactivity"

然后可以在ViewModel中处理此问题:

    public MainWindowViewModel()
    {
        PassSelectedValueToXmlCommand = new DelegateCommand<string>(HandleCommand);
    }

    public ICommand PassSelectedValueToXmlCommand { get; set; }

    private void HandleCommand(string parameter)
    {
        MessageBox.Show(String.Format("Parameter with value {0} was received by ViewModel", parameter));
    }
public主窗口视图模型()
{
PassSelectedValueToXmlCommand=新的DelegateCommand(HandleCommand);
}
public ICommand passselectedvaluetoxml命令{get;set;}
私有void HandleCommand(字符串参数)
{
Show(String.Format(“ViewModel接收到值为{0}的参数”,参数));
}

对于这种情况,您甚至不需要转换器,但您不会处理以这种方式更改的组合框选择。如果您也需要,那么您可以将绑定值“保存”到hidden TextBlock.Tag或StackPanel的标记或类似的内容。我相信你现在能弄明白。

嗨,珍妮兹,我做了类似的事情,但问题出在后面。我正在用我的单选按钮进行多重绑定。已检查。因此,现在当我选择该单选按钮时,我需要获取combobox的值,并将其传递给我的XML,这发生在ConvertBack:)
    public MainWindowViewModel()
    {
        PassSelectedValueToXmlCommand = new DelegateCommand<string>(HandleCommand);
    }

    public ICommand PassSelectedValueToXmlCommand { get; set; }

    private void HandleCommand(string parameter)
    {
        MessageBox.Show(String.Format("Parameter with value {0} was received by ViewModel", parameter));
    }