C# 使用IValueConverter类更改文本框的背景
你好,我有一个类来改变文本框的背景颜色,这取决于它的文本。 如果文本为空,则为黄色,否则为透明文本。 但它不起作用C# 使用IValueConverter类更改文本框的背景,c#,C#,你好,我有一个类来改变文本框的背景颜色,这取决于它的文本。 如果文本为空,则为黄色,否则为透明文本。 但它不起作用 namespace Contrats_Congeles.Library { [ValueConversion(typeof(string),typeof(SolidColorBrush))] public class BackgroundConverter_Yellow : IValueConverter { public Brush
namespace Contrats_Congeles.Library
{
[ValueConversion(typeof(string),typeof(SolidColorBrush))]
public class BackgroundConverter_Yellow : IValueConverter
{
public Brush YellowBrush { get; set; }
public Brush TransparentBrush { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value==null)
{
return YellowBrush;
}
else
{
return TransparentBrush;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
然后在XAML部分:
<Page.Resources>
<local1:BackgroundConverter_Yellow x:Key="BackgroundConveter_Yellow"
YellowBrush="Yellow"
TransparentBrush="Transparent"/>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="5"/>
<Setter Property="CharacterCasing" Value="Upper"/>
</Style>
<Style TargetType="{x:Type DatePicker}">
<Setter Property="Margin" Value="5"/>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Margin" Value="5"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type GroupBox}">
<Setter Property="Padding" Value="1"/>
<Setter Property="BorderBrush" Value="Gray"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="Margin" Value="5"/>
</Style>
</Page.Resources>
在文本框中:
<TextBox x:Name="PdsNetTxtBox" Grid.Row="1" Grid.Column="1"
PreviewKeyDown="PdsNetTxtBox_PreviewKeyDown"
Background="{Binding Path=Source,
Converter={StaticResource BackgroundConveter_Yellow}}"
KeyUp="PdsNetTxtBox_KeyUp"/>
但即使文本框的文本为空,也不会发生任何更改。
感谢您的帮助您的错误来自test value==null:(empty不同于null)和您的绑定 我建议你增加对绳子长度的测试
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//test the length of string
if (value == null || value.ToString().Length == 0)
{
return YellowBrush;
}
else
{
return TransparentBrush;
}
}
这里是我在文本框中的绑定:
<TextBox x:Name="PdsNetTxtBox"
Background="{Binding RelativeSource={RelativeSource self},Path=Text, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BackgroundConveter_Yellow}}"/>
XAML:
写在手机上的p.S
编辑:当文本属性为空时,您可能需要测试该值。“value”参数为null或string.Empty。如果是string.Empty,则必须添加“If(value==null)return Binding.DoNothing;”在Convert方法的开头我认为如果您交换颜色,您的文本框将始终为黄色!。我建议你添加另一个测试->查看我的编码可能是你的绑定,查看我的我已经放了我的完整样本,可以了如果这个答案对你有帮助,请不要忘记UVP注释/验证答案
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
xmlns:local1="clr-namespace:Contrats_Congeles.Library"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local1:BackgroundConverter_Yellow x:Key="BackgroundConveter_Yellow"
YellowBrush="Yellow"
TransparentBrush="Transparent"/>
</Window.Resources>
<Grid>
<TextBox x:Name="PdsNetTxtBox"
Background="{Binding RelativeSource={RelativeSource self},Path=Text, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BackgroundConveter_Yellow}}"/>
</Grid>
</Window>
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
namespace Contrats_Congeles.Library
{
[ValueConversion(typeof(string), typeof(SolidColorBrush))]
public class BackgroundConverter_Yellow : IValueConverter
{
public Brush YellowBrush { get; set; }
public Brush TransparentBrush { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || value.ToString().Length == 0)
{
return YellowBrush;
}
else
{
return TransparentBrush;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
<Page.Resources>
<converterns:TextToBackgroundConverter x:Key="TextToBackgroundConverter"/>
</Page.Resources>
<TextBox Background="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource TextToBackgroundConverter}, Mode=OneWay}">
public class TextToBackgroundConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
string stringval = value.ToString();
if(!string.IsNullOrEmpty(stringval)) return Brushes.Transparent;
else return Brushes.Yellow;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return Binding.DoNothing;
}
}