开发者

Automatic data update in WPF

Automatic data update in WPF

<Window x:Class="Binding2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xa开发者_C百科ml/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>

I have two text boxes. How can I program that the value in Destination text box is automatically modified based on the value in Source text box? For example, when the value in Source is "abc", how can I make the value in Destination is automaticaly "x" + "abc" + "x"? Or, make destination the number 10*source.

ADDED

I also found a way to get the result when the source is more than one - Bind an element to two sources


That's easy using 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"; }
    }

}

Use that class as the DataContext for your view, and bind one TextBox to Source (TwoWay), and the other to Destination (OneWay):

<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" Text="{Binding Source, Mode=TwoWay}" />
    </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" Text="{Binding Destination, Mode=OneWay}" IsReadOnly="True" />
    </StackPanel>
</StackPanel>

Anyway, if you're building a non-trivial WPF application, I strongly recommend moving to the MVVM pattern, in the long-term your app will be much easier to maintain.


There are a couple of approaches you could use, the first is purely within the UI by using Elementname binding:

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

This will synchronize the TextBoxes, add a ValueConverter if you want to add text before and after. For example:

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

And used as follows:

<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}}"/>

The other approach would be to create a ViewModel layer than binds to your view and performs this logic.


quickest way would be to use ValueConverter or have 2 separate properties in your ViewModel so that one is bound to Source Property in VM, and other derives from the Source (i.e. decorates it with "X" and returns it)

Example(you should implement INotifyPropertyChanged so that Binding engine picks up the Destination change and refreshed the textbox in your GUI):

public string Source { get; set; }

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


You can use StringFormat, i.e.: StringFormat='x{0}x', e.g.:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜