开发者

MVVM WPF Question

I am using MVVM pattern to implement a WPF App. My question is, I have a Yes and No Radio Buttons and a text box.

ex: Do you own a Car: Yes , No (Radio Buttons) Enter Model:______________ (Text Box)

if user selects No, i am disabling it. if user selects Yes and leaves 'Enter Model' Textbox empty, i want to display a message or change background(like the way AdornerElement does). How can i achieve this.

Thank you, Re@开发者_运维问答@y.


What I would do, if your desperate to use MVVM, is create a number of custom type converters (http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx).

So on a Textblock, bind the text to the IsChecked property of the radio buttons, then using the converter, translate that boolean into a custom string that you want to display.

For the background of the form, bind the Background element to the appropriate control's IsChecked and use another type converter to convert from bool to Color.

Thats, in my understanding, how to do it via MVVM.


Actually you don't need to apply MVVM for this particular problem, since it is totally a UI related thing. You can have IsEnabled property bind to the yesRadiobutton.IsChecked property to make it disable while you select noRasioButton. And again You can use BoolToVisibilityConverter and bind the same to a TextBlock to display a message. Same for a Rectangle with particular background


I would implement this using triggers only. No need to complicate things by using MVVM when you don't need to.


This is not a part of MVVM Pattern, Use a WPF Trigger as follows, for the background color use a converter`

 <Window.Resources>
    <conv:BackgroundConverter x:Key="backgroundConverter"/>
    <Style TargetType="{x:Type TextBox}" x:Key="ModelBoxStyle">
        <Setter Property="IsEnabled" Value="True"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=NoOption,Path=IsChecked}" Value="True" >
                <Setter Property="IsEnabled" Value="False"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid Background="{Binding Text,ElementName=ModelBox,Converter={StaticResource backgroundConverter}}">
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <RadioButton Content="Yes" x:Name="YesOption" Grid.Column="0"/>
    <RadioButton Content="No" x:Name="NoOption" Grid.Column="2"/>
    <TextBlock Text="Enter Model :" Grid.Row="1" Grid.Column="0"/>
    <TextBox x:Name="ModelBox" MinWidth="100" Height="20" Grid.Row="1" Grid.Column="2" Style="{StaticResource ModelBoxStyle}"
     HorizontalAlignment="Center" VerticalAlignment="Top"/>
</Grid>


 public class BackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string text = value as string;
        if (string.IsNullOrEmpty(text))
        {
            return Brushes.Red;
        }
        return Brushes.White;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜