如何使用简单的MVVM结构进行数据绑定

时间:2016-11-15 13:49:22

标签: wpf mvvm datatrigger

在得到正确答案之后,我认为我应该更新这个以显示人们未来参考的工作代码:

MainWindow.xaml

<Grid>
    <Grid.Resources>
        <local:ValueConverters x:Key="ValueConverters"></local:ValueConverters>
    </Grid.Resources>
    <TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}">
        <TextBox.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=Text,Converter={StaticResource ValueConverters}}" Value="True">
                        <Setter Property="TextBox.Foreground" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</Grid>

MainWindow.xaml.cs

public partial class MainWindow : MetroWindow
{
     private readonly DataBindingViewModel _vm = new DataBindingViewModel();

    public MainWindow()
    {
        InitializeComponent();
        DataContext = _vm;
    }
}

DataBindingViewModel.cs

public class DataBindingViewModel : INotifyPropertyChanged
{
    private string _text;

    public string Text
    {
        get
        {
            return this._text;
        }
        set
        {
            this._text = value;
            if (null != PropertyChanged)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Text"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

ValueConverters

public class ValueConverters : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (null != value)
        {
            if (value.ToString() == "1")
                return true;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
< p>以上现在有效:)

2 个答案:

答案 0 :(得分:2)

在MainWindow的XAML中,DataContext设置为ValueConverters的一个实例:

<Window.DataContext>
    <local:ValueConverters/>
</Window.DataContext>

这似乎没有意义。你应该用

替换它
<Window.DataContext>
    <local:DataBindingViewModel/>
</Window.DataContext>

您不必在MainWindow的构造函数中再次设置DataContext,但可能仍然分配私有字段(供以后使用),如下所示:

private readonly DataBindingViewModel _vm;

public MainWindow()
{
    InitializeComponent();
    _vm = (DataBindingViewModel)DataContext;
}

或者,不要在XAML中设置DataContext,并在后面的代码中创建它,如下所示:

private readonly DataBindingViewModel _vm = new DataBindingViewModel();

public MainWindow()
{
    InitializeComponent();
    DataContext = _vm;
}

答案 1 :(得分:2)

您可以在没有转换器的情况下更改某些值的前景颜色。使用触发器将Text值与已知字符串进行比较,并在匹配

时更改Foreground
<TextBox Grid.Row="2" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.Style>
        <Style>
            <Style.Triggers>
                <Trigger Property="TextBox.Text" Value="1">
                    <Setter Property="TextBox.Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>