WPF中的自动数据更新

时间:2011-07-21 14:18:15

标签: c# .net wpf

enter image description here

<Window x:Class="Binding2.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">
    <StackPanel Orientation="Vertical">
        <StackPanel HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="165" Orientation="Horizontal">
            <TextBlock Height="20" Name="_sourceTextBlock" Text="Source" Width="67" />
            <TextBox Height="20" Name="_sourceTextBox" Width="92" />
        </StackPanel>
        <StackPanel HorizontalAlignment="Left" Name="stackPanel2" VerticalAlignment="Top" Width="165" Orientation="Horizontal">
            <TextBlock Height="20" Name="_destTextBlock" Text="Destination" Width="67" />
            <TextBox Height="20" Name="_destTextBox" Width="92" />
        </StackPanel>
    </StackPanel>
</Window>

我有两个文本框。如何根据“源”文本框中的值自动修改“目标”文本框中的值?例如,当Source中的值为“abc”时,如何使Destination中的值自动为“x”+“abc”+“x”?或者,将目的地设为10 *源。

ADDED

当源不止一个时,我也找到了获得结果的方法 - Bind an element to two sources

4 个答案:

答案 0 :(得分:4)

使用MVVM

很容易
public class MainWindowViewModel : ViewModelBase
{
    private string _source;
    public string Source
    {
        get { return _source; }
        set
        {
            _source = value;
            OnPropertyChanged("Source");
            OnPropertyChanged("Destination");
        }
    }

    public string Destination
    {
        get { return "x" + _source + "x"; }
    }

}

将该类用作视图的DataContext,并将一个TextBox绑定到SourceTwoWay),将另一个绑定到Destination({ {1}}):

OneWay

无论如何,如果您正在构建一个非平凡的WPF应用程序,我强烈建议您转移到MVVM模式,从长远来看,您的应用程序将更容易维护。

答案 1 :(得分:3)

您可以使用几种方法,第一种方法纯粹在UI中使用Elementname绑定:

<TextBox Height="20" Name="_destTextBox"
         Text="{Binding Path=Text, ElementName=_sourceTextBox}"/>

这将同步TextBoxes,如果要在之前和之后添加文本,请添加ValueConverter。例如:

public class MyValueConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return  "x" + (string)value + "x";
    }

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

使用如下:

<Window x:Class="Binding2.MainWindow"
    ....
    xmlns:local="clr-namespace:Binding2"
    ....


<Window.Resources>
   <MyValueConverter x:Key="MyValueConverter"/>
</Window.Resources>

...


<TextBox Height="20" Name="_destTextBox"
         Text="{Binding Path=Text, ElementName=_sourceTextBox, Converter={StaticResource MyValueConverter}}"/>

另一种方法是创建一个ViewModel层而不是绑定到您的视图并执行此逻辑。

答案 2 :(得分:2)

最快的方法是使用ValueConverter或在ViewModel中有2个单独的属性,以便一个绑定到VM中的Source Property,而另一个派生自Source(即用“X”装饰它并返回它)

示例(您应该实现INotifyPropertyChanged,以便绑定引擎获取目标更改并刷新GUI中的文本框):

public string Source { get; set; }

public string Destination {get{
    return "X" + Source + "X"
}
}

答案 3 :(得分:1)

您可以使用StringFormat,即:StringFormat='x{0}x',例如:

<TextBox Height="20" Name="_destTextBox" Width="92" 
            Text="{Binding ElementName=_sourceTextBox, Path=Text, StringFormat='x{0}x'}" />