0

我有一个简单的 XAML,如下所示:

<Window.Resources>
    <Converters:ButtonVisibilityConverter x:Key="m_ButtonConverter"/>
</Window.Resources>

<Grid>
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="71,18,0,0" Name="comboBox1" VerticalAlignment="Top" Width="229" SelectionChanged="comboBox1_SelectionChanged">              
    </ComboBox>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="71,76,0,0" Name="textBox1" VerticalAlignment="Top" Width="229" Style="{StaticResource requiredFieldValidationStyle}"/>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="71,125,0,0" Name="textBox2" VerticalAlignment="Top" Width="229" Style="{StaticResource requiredFieldValidationStyle}"/>        
    <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="225,175,0,0" Name="button1" VerticalAlignment="Top" Width="75">
        <Button.IsEnabled>
            <MultiBinding Converter="{StaticResource m_ButtonConverter}">
                <Binding ElementName="textBox1" Path="Text" />
                <Binding ElementName="textBox2" Path="Text" />
                <Binding ElementName="comboBox1" Path="Text" />
            </MultiBinding>
        </Button.IsEnabled>
    </Button>
    <Label Content="Box 1" Height="28" HorizontalAlignment="Left" Margin="12,74,0,0" Name="label1" VerticalAlignment="Top" />
    <Label Content="Box 2" Height="28" HorizontalAlignment="Left" Margin="12,123,0,0" Name="label2" VerticalAlignment="Top" />
</Grid>

textbox1 和 textbox2 现在都是必填字段,除非两个框都有文本,否则该按钮不会启用。

我想做以下事情:在组合框中选择偶数时,我想将 textbox2 条目设为可选。这意味着,我将它从(按钮的 IsEnabled 的)多重绑定中删除,并且还删除了样式。However, when an odd number is chosen, I want them back.

有人可以帮忙吗?

4

2 回答 2

0

我认为更改MultiBinding基于可选条目将是有问题的。我的建议是使用 ViewModel 并为 IsButtonEnabled 创建一个依赖属性,并将可选条目和验证的逻辑放在 ViewModel 中。然后你可以绑定到IsButtonEnabledDP。

于 2013-02-21T17:29:22.927 回答
0

我可以通过有条件地检查组合框的 selectionchanged 事件的 xaml.cs 文件来做到这一点。

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selVal = (int)comboBox1.SelectedValue;

        if ((selVal % 2) == 0)
        {
            // remove the style
            textBox2.Style = null;

            // remove from the button's IsEnabled multibinding                 
            _vfs.NumberValidationFlag = false;
            BindingOperations.ClearBinding(button1, Button.IsEnabledProperty);
            BindingOperations.SetBinding(button1, Button.IsEnabledProperty, GetBindingForButton());                
        }
        else
        {
            // add back the style
            Style myStyle = (Style)Application.Current.Resources["requiredFieldValidationStyle"];
            textBox2.Style = myStyle;

            // add back to the button's IsEnabled multibinding                
            _vfs.NumberValidationFlag = true;
            BindingOperations.ClearBinding(button1, Button.IsEnabledProperty);
            BindingOperations.SetBinding(button1, Button.IsEnabledProperty, GetBindingForButton());
        }
    }
于 2013-02-26T22:04:11.353 回答