c# - 如何调用具有数据绑定(bind)的通用方法?

标签 c# .net data-binding mvvm

我想了解当我们使用许多属性时如何正确使用 MVVM 和数据绑定(bind)。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="463" Text="{Binding OriginalText, UpdateSourceTrigger=PropertyChanged}" />
    <Label Height="28" HorizontalAlignment="Left" Margin="12,242,0,0" Name="label1" VerticalAlignment="Top" Width="463" Content="{Binding ModifiedText}"/>
    <CheckBox Content="Upper" Height="16" HorizontalAlignment="Left" Margin="12,41,0,0" Name="checkBox1" VerticalAlignment="Top" />
    <CheckBox Content="Underline" Height="16" HorizontalAlignment="Left" Margin="12,63,0,0" Name="checkBox2" VerticalAlignment="Top" />
    <CheckBox Content="Bold" Height="16" HorizontalAlignment="Left" Margin="12,85,0,0" Name="checkBox3" VerticalAlignment="Top" />
    <CheckBox Content="Shadow" Height="16" HorizontalAlignment="Left" Margin="12,107,0,0" Name="checkBox4" VerticalAlignment="Top" />
    <CheckBox Content="Red" Height="16" HorizontalAlignment="Left" Margin="12,129,0,0" Name="checkBox5" VerticalAlignment="Top" />
    <CheckBox Content="Scary" Height="16" HorizontalAlignment="Left" Margin="12,151,0,0" Name="checkBox6" VerticalAlignment="Top" />
    <CheckBox Content="Remove first letter" Height="16" HorizontalAlignment="Left" Margin="12,173,0,0" Name="checkBox7" VerticalAlignment="Top" />
    <CheckBox Content="Remove last letter" Height="16" HorizontalAlignment="Left" Margin="12,195,0,0" Name="checkBox8" VerticalAlignment="Top" />
</Grid>

我有一个 OriginalText 文本框和一个 ModifiedText 标签。当我选中一个框时,我想直接应用修改而无需单击按钮。我该怎么做?

在我的 ViewModel 中,我创建了绑定(bind)到 XAML CheckBox 的所有属性。
    private string _originalText = string.Empty;
    public string OriginalText
    {
        get { return _originalText; }
        set
        {
            _originalText = value;
            NotifyPropertyChanged("OriginalText");
        }
    }

    private string _modifiedText;
    public string ModifiedText
    {
        get { return _originalText; }
        set
        {
            _originalText = value;
            NotifyPropertyChanged("ModifiedText");
        }
    }

    private bool upper;
    public bool Upper
    {
        get { return upper; }
        set
        {
            upper = value;
            NotifyPropertyChanged("Upper");
            // Should I notify something else here or call a refresh method?
        }
    }

    private bool removeFirstLetter;
    public bool RemoveFirstLetter
    {
        get { return removeFirstLetter; }
        set
        {
            removeFirstLetter = value;
            NotifyPropertyChanged("RemoveFirstLetter");
            // Should I notify something else here or call a refresh method?
        }
    }

    // ...

然后我此时在同一个 ViewModel 类中创建了一个 Work 方法。稍后我会将此方法应用到业务中。
private void Work()
{ 
    string result = _originalText;
    if (Upper)
        result = result.ToUpper();
    if (removeFirstLetter)
        result = result.Substring(1, result.Length);
    // if ...
    ModifiedText = result; 
}

我的问题是我应该在什么时候、在哪里调用工作方法?我应该在每个 setter 或 getter 中调用它吗?我不喜欢这个主意。我做错了什么...

谢谢你。

最佳答案

在您的特定情况下,您应该使用 INotifyPropertyChanged 接口(interface)创建一个 bool 属性。现在将此属性绑定(bind)到您的“IsChecked”复选框属性。通过在 setter 中调用 Work() 方法,每次“勾选”复选框时,setter 都会触发。

关于c# - 如何调用具有数据绑定(bind)的通用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11526349/

相关文章:

c# - 混合 ASP.NET 和 MVC 路由

c# - 程序 '[12460] TestProject.exe' 已退出,代码为 -1 (0xffffffff)

c# - 在 .NET 4.0 中创建可与 .NET 4.5 中的 "await"一起使用的异步方法

c# - 为什么此代码无法编译,尽管接口(interface)是引用类型?

c# - 什么更好用: a DataGrid or ListView for displaying large amounts of data?

c# - 无法收到用户的任何 Discord 消息

c# - 从 C++/CLI 函数调用 C# 函数

javascript - AngularJS - 在 ng-click 中使用数据绑定(bind) {{}}

java - Android 数据绑定(bind) NoSuchMethodError

c# - 我的 ItemsControl 和数据绑定(bind)有什么问题?