如何让数据绑定与简单的 MVVM 结构一起工作

How to get data binding to work with a simple MVVM structure

得到正确答案后,我想我应该更新这个以显示工作代码供人们将来参考:

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;
}

价值转换器

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;
    }
}

以上现在有效:)

在您的 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;
}

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

<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>