我尝试根据内容更改WPF中文本框的背景颜色。
使用断点,我看到我的ValueConverter被构造,但转换和转换方法都没有被调用,因此样式不起作用。
' LightBlue' XAML中的样式在启动时确实有效。
我尝试使用Snoop,但我不知道我在寻找什么。
... XAML
<Grid.Resources>
<local:ThreadCreationLimitChanged x:Key="ThreadCreationLimitChanged"></local:ThreadCreationLimitChanged>
</Grid.Resources>
<Label Grid.Row="0" Grid.Column="0" Margin="0, 5, 0, 0" Content="New Thread Limit"></Label>
<TextBox Grid.Row="0" Grid.Column="1" Margin="10, 5, 10, 0" Width="100" Text="{Binding Path=ManagerConfig.ThreadCreationLimit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<TextBox.Style>
<Style>
<Setter Property="TextBox.Background" Value="LightBlue"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text,Converter={StaticResource ThreadCreationLimitChanged}}" Value="false">
<Setter Property="TextBox.Background" Value="Yellow"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
... ValueConverter
public class ThreadCreationLimitChanged : IValueConverter
{
public ThreadCreationLimitChanged()
{
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.ToString() == "120";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
一点类ManagerConfig ...(NotifyPropertyChanged实现了INotifyPropertyChanged)
class ManagerConfig : NotifyPropertyChanged
{
private uint _ThreadCreationLimit;
public uint ThreadCreationLimit
{
get { return _ThreadCreationLimit; }
set
{
_ThreadCreationLimit = value;
OnPropertyChanged("ThreadCreationLimit");
}
}
答案 0 :(得分:2)
为什么不在ManagerConfig
上创建计算属性来报告ThreadCreationLimit
更改的状态?那样一个人不需要转换器。代码如下:
public bool IsOverLimit { get { return ThreadCreationLimit > 120 } }
public uint ThreadCreationLimit
{
get { return _ThreadCreationLimit; }
set
{
_ThreadCreationLimit = value;
OnPropertyChanged("ThreadCreationLimit");
OnPropertyChanged("IsOverLimit");
}
}
然后将数据触发器绑定到IsOverLimit
,每次设置/更改ThreadCreationLimit
时都会发送更改通知。
另外一个好处是业务逻辑在目标类而不是转换器上。
答案 1 :(得分:1)
我猜你有Binding错误。如下所示进行检查,
1)直接访问TextBox以获取Value,
<Grid.Resources>
<local:ThreadCreationLimitChanged x:Key="ThreadCreationLimitChanged"></local:ThreadCreationLimitChanged>
</Grid.Resources>
<Label Grid.Row="0" Grid.Column="0" Margin="0, 5, 0, 0" Content="New Thread Limit"></Label>
<TextBox x:Name="tBox" Grid.Row="0" Grid.Column="1" Margin="10, 5, 10, 0" Width="100" Text="{Binding Path=ManagerConfig.ThreadCreationLimit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<TextBox.Style>
<Style>
<Setter Property="TextBox.Background" Value="LightBlue"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, ElementName=tBox,Converter={StaticResource ThreadCreationLimitChanged}}" Value="false">
<Setter Property="TextBox.Background" Value="Yellow"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
2)获取正确的模型值,
<Grid.Resources>
<local:ThreadCreationLimitChanged x:Key="ThreadCreationLimitChanged"></local:ThreadCreationLimitChanged>
</Grid.Resources>
<Label Grid.Row="0" Grid.Column="0" Margin="0, 5, 0, 0" Content="New Thread Limit"></Label>
<TextBox Grid.Row="0" Grid.Column="1" Margin="10, 5, 10, 0" Width="100" Text="{Binding Path=ManagerConfig.ThreadCreationLimit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<TextBox.Style>
<Style>
<Setter Property="TextBox.Background" Value="LightBlue"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ManagerConfig.ThreadCreationLimit,Converter={StaticResource ThreadCreationLimitChanged}}" Value="false">
<Setter Property="TextBox.Background" Value="Yellow"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
答案 2 :(得分:1)
将此{Binding Path=Text,Converter={StaticResource ThreadCreationLimitChanged}}
更改为{Binding Path=Text, RelativeSource={RelativeSource Self} ,Converter={StaticResource ThreadCreationLimitChanged}}