C# WPF中的数据自动更新
我有两个文本框。如何编程使目标文本框中的值根据源文本框中的值自动修改?例如,当源中的值为abc时,如何使目标中的值自动为x+abc+x?或者,将目标编号设为10*源 补充C# WPF中的数据自动更新,c#,.net,wpf,C#,.net,Wpf,我有两个文本框。如何编程使目标文本框中的值根据源文本框中的值自动修改?例如,当源中的值为abc时,如何使目标中的值自动为x+abc+x?或者,将目标编号设为10*源 补充 我还发现了一种在源不止一个时获得结果的方法-最快的方法是使用ValueConverter或在ViewModel中有两个单独的属性,以便一个绑定到VM中的源属性,另一个从源派生,即用X修饰它并返回它 示例您应该实现INotifyPropertyChanged,以便绑定引擎获取目标更改并刷新GUI中的文本框: <Window
我还发现了一种在源不止一个时获得结果的方法-最快的方法是使用ValueConverter或在ViewModel中有两个单独的属性,以便一个绑定到VM中的源属性,另一个从源派生,即用X修饰它并返回它 示例您应该实现INotifyPropertyChanged,以便绑定引擎获取目标更改并刷新GUI中的文本框:
<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>
最快的方法是使用ValueConverter,或者在ViewModel中有两个单独的属性,以便一个绑定到VM中的源属性,另一个从源属性派生,即用X修饰它并返回它 示例您应该实现INotifyPropertyChanged,以便绑定引擎获取目标更改并刷新GUI中的文本框:
<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>
您可以使用两种方法,第一种是通过使用Elementname绑定在UI中:
public string Source { get; set; }
public string Destination {get{
return "X" + Source + "X"
}
}
并按如下方式使用:
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();
}
}
另一种方法是创建一个ViewModel层,而不是绑定到视图并执行此逻辑。可以使用两种方法,第一种方法纯粹是在UI中使用Elementname绑定:
public string Source { get; set; }
public string Destination {get{
return "X" + Source + "X"
}
}
并按如下方式使用:
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();
}
}
另一种方法是创建一个ViewModel层,然后绑定到视图并执行此逻辑。这很容易使用:
将该类用作视图的DataContext,并将一个文本框双向绑定到源,另一个单向绑定到目标:
无论如何,如果您正在构建一个非平凡的WPF应用程序,我强烈建议您转向MVVM模式,从长远来看,您的应用程序将更易于维护。这很容易使用:
将该类用作视图的DataContext,并将一个文本框双向绑定到源,另一个单向绑定到目标:
无论如何,如果您正在构建一个非平凡的WPF应用程序,我强烈建议您转向MVVM模式,从长远来看,您的应用程序将更易于维护。您可以使用StringFormat,即:StringFormat='x{0}x',例如:
您可以使用StringFormat,即:StringFormat='x{0}x',例如:
+1 ValueConverter是一个有趣的问题。@anvarbek raupov:您能详细介绍一下使用INotifyPropertyChanged的代码吗?它看起来像是我在谷歌上搜索过的那种。@prosseek:请用谷歌搜索一下,这里有很多例子,这是一种老式的方式,当您的数据/业务对象发生变化时通知您的消费者。WPF引入了依赖属性,但INotifyPropertyChanged又称INPC在MVVM中仍然广泛使用,在MVVM中,您可以查看和查看模型,并且可以通过INPC+1将ViewModel中的更改发送回视图。ValueConverter是一个有趣的问题。@anvarbek raupov:您能详细介绍一下使用INotifyPropertyChanged的代码吗?它看起来像是我在谷歌上搜索过的那种。@prosseek:请用谷歌搜索一下,这里有很多例子,这是一种老式的方式,当您的数据/业务对象发生变化时通知您的消费者。WPF引入了依赖属性,但INotifyPropertyChanged又称INPC在MVVM中仍然广泛使用,在MVVM中,您可以查看和查看模型,并且ViewModel中的更改通过INPCF被激发回视图,或者只需在文本之前/之后添加我建议的绑定的StringFormat参数。@ColinE:您能详细介绍一下添加值转换器吗?谢谢。@ColinE:我需要添加xmlns:local=clr名称空间:Binding2才能工作。谢谢为了简单地在文本之前/之后添加,我建议为绑定使用StringFormat参数。@ColinE:您能详细介绍一下添加ValueConverter吗?谢谢。@ColinE:我需要添加xmlns:local=clr名称空间:Binding2才能工作。谢谢
<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>
<TextBox Height="20" Name="_destTextBox" Width="92"
Text="{Binding ElementName=_sourceTextBox, Path=Text, StringFormat='x{0}x'}" />