有条件地修改样式和多重绑定

时间:2013-02-21 17:09:51

标签: c# validation xaml styles multibinding

我有一个简单的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)中删除它,并删除样式。但是,当选择奇数时,我希望它们回来。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我认为根据可选条目更改MultiBinding会有问题。我的建议是使用ViewModel并为IsButtonEnabled创建一个依赖属性,并在ViewModel中放置可选入口和验证的逻辑。然后你就可以绑定到IsButtonEnabled DP。

答案 1 :(得分: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());
        }
    }