wpf 当文本更改并满足特定条件时更改文本框的前景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16986105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change foreground color of textbox when text changes and meets certain criterion
提问by Aarohi S
I require to set text color when text changes inside textbox and meets certain criterion. I can implement this from code behind with textbox_textchangedevent and set brushes.colorto desired color.
当文本框内的文本更改并满足某些条件时,我需要设置文本颜色。我可以使用textbox_textchanged事件从后面的代码中实现这一点,并将 Brushes.color 设置为所需的颜色。
But I am not being able to implement this with xaml wpf approach. I am new to wpf, I'm not sure how can I set text color depending upon certain criterion when text changes in textbox.
但是我无法使用 xaml wpf 方法来实现这一点。我是 wpf 的新手,当文本框中的文本更改时,我不确定如何根据某些标准设置文本颜色。
For example: For a given textbox, when text changes, it needs to determine if input text is a number then change foreground color to green else red.
例如:对于给定的文本框,当文本更改时,它需要确定输入的文本是否为数字,然后将前景色更改为绿色,否则为红色。
Looking forward for the help. Thank you in advance.
期待帮助。先感谢您。
采纳答案by Colin
I am not sure whether a binding converter is allowed in your situation. But here is a solution which only needs a binding converter in your code behind.
我不确定您的情况是否允许使用绑定转换器。但这里有一个解决方案,它只需要在你的代码后面有一个绑定转换器。
Here is the code in xaml
这是xaml中的代码
<Grid.Resources>
<local:ValueConverter x:Key="ValueConverter"></local:ValueConverter>
</Grid.Resources>
<TextBox Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}">
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text,Converter={StaticResource ValueConverter}}" Value="True">
<Setter Property="TextBox.Foreground" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Here is the view model and the value converter
这是视图模型和值转换器
public class ViewModel : 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 ValueConverter : 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;
}
}
So the solution uses the data trigger to fulfill the goal. The only reason for using binding converter here is that you need a place to determine what kind of value should change the foreground of the TextBox. Here the foreground of TextBox will be red when the value of the TextBox is "1".
因此该解决方案使用数据触发器来实现目标。在这里使用绑定转换器的唯一原因是您需要一个地方来确定应该更改 TextBox 前景的值类型。这里当 TextBox 的值为“1”时,TextBox 的前景将是红色的。
回答by Jesse Carter
You should just be able to plug into the TextChangedevent in wpf and bind a method to this event in the XAML. Then you could check to see if the new values meets your criteria and change the color accordingly.
您应该能够插入TextChangedwpf 中的事件并将方法绑定到 XAML 中的此事件。然后您可以检查新值是否符合您的标准并相应地更改颜色。
I'm not really sure what you mean by the "XAML approach" but in this case when you simply want to attach behavior to an event thats raised on one of your controls I do not think that it is wrong to do it the way you have already tried using TextChanged. That's why events are visible in XAML in the first place.
我不太确定您所说的“XAML 方法”是什么意思,但在这种情况下,当您只想将行为附加到在您的一个控件上引发的事件时,我认为按照您的方式进行操作并没有错已经尝试使用TextChanged. 这就是为什么事件首先在 XAML 中可见的原因。
回答by user2369405
Check the length of the string in the textbox that is being written on every input. If it is >10 or whatever you want it to be, then change colour. You could also make that trigger a button that was greyed out.
检查在每个输入上写入的文本框中字符串的长度。如果它大于 10 或任何你想要的,然后改变颜色。您还可以使触发按钮变灰。
Sample:
样本:
MyTextBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return new Size(MyTextBlock.DesiredSize.Width, MyTextBlock.DesiredSize.Height);
回答by Dan
Pure xaml? , you might want to look at interactivity, Interaction, Triggers ?
纯xaml?,您可能想查看交互性、交互性、触发器?
Using EventTrigger in XAML for MVVM – No Code Behind
在 XAML 中为 MVVM 使用 EventTrigger – 没有背后的代码
IMMO I think is better to hook up to code properties/converters/extensions,etc ... for better code reuse , but of course subjective to opinions... and at the end is always up to you.
恕我直言,我认为最好连接到代码属性/转换器/扩展等......以获得更好的代码重用,但当然主观意见......最后总是取决于你。

