【发布时间】:2019-03-26 08:18:33
【问题描述】:
我试图让我的矩形根据我的树滑块改变颜色。 B、R 和 G。
代码流程: 滑块绑定到 colorClass 女巫设置的 g、r 和 b 值,具体取决于幻灯片。
每当这些道具中的一个发生变化时,它都会调用一个事件来更新结果颜色 矩形颜色由 resultcolor 设置
所以理论上,当您拖动滑块时,矩形应该会切换颜色。 但它没有
我收到此错误。
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='#FF0000FF' BindingExpression:Path=Result; DataItem='VM' (HashCode=64479624); target element is 'Rectangle' (Name=''); target property is 'Fill' (type 'Brush')
这是我的 XAML:
<Label VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="1" Grid.Column="0" FontSize="24">R</Label>
<Slider Maximum="255" Name="RSlider" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Value="{Binding Path=RedValue, Mode=TwoWay}"></Slider>
<TextBox PreviewTextInput="NumberValidationTextBox" Grid.Row="1" Grid.Column="2" Height="auto" Text="{Binding Path=RedValue, Mode=TwoWay}"></TextBox>
<Label VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="2" Grid.Column="0" FontSize="24">G</Label>
<Slider Maximum="255" Name="GSlider" VerticalAlignment="Center" Grid.Row="2" Grid.Column="1" Value="{Binding Path=GreenValue, Mode=TwoWay}"></Slider>
<TextBox PreviewTextInput="NumberValidationTextBox" Grid.Row="2" Grid.Column="2" Text="{Binding Path=GreenValue, Mode=TwoWay}"></TextBox>
<Label VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="3" Grid.Column="0" FontSize="24">B</Label>
<Slider Maximum="255" Name="BSlider" VerticalAlignment="Center" Grid.Row="3" Grid.Column="1" Value="{Binding Path=Color.BlueValue, Mode=TwoWay}"></Slider>
<TextBox PreviewTextInput="NumberValidationTextBox" Grid.Row="3" Grid.Column="2" Text="{Binding Path=Color.BlueValue, Mode=TwoWay}"></TextBox>
<Rectangle Grid.Column="3" Grid.Row="1" Grid.RowSpan="3" Fill="{Binding Path=Result}"></Rectangle>
我有一个要保存在数据库中的颜色类。 它有 3 个 int 属性 B、G 和 R。这是类:
public class ColorModel : INotifyPropertyChanged
{
private int _GreenValue;
private int _RedValue;
private int _BlueValue;
public int GreenValue
{
get { return _GreenValue; }
set
{
if (_GreenValue != value)
{
_GreenValue = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("GreenValue"));
}
}
}
}
public int RedValue
{
get { return _RedValue; }
set
{
if (_RedValue != value)
{
_RedValue = value;
PropertyChanged(this, new PropertyChangedEventArgs("RedValue"));
}
}
}
public int BlueValue
{
get { return _BlueValue; }
set
{
if (_BlueValue != value)
{
_BlueValue = value;
PropertyChanged(this, new PropertyChangedEventArgs("BlueValue"));
}
}
}
public Color GetColor()
{
Color result = new Color();
result = Color.FromScRgb(1, RedValue, GreenValue, BlueValue);
return result;
}
public event PropertyChangedEventHandler PropertyChanged;
}
GetColor 方法只是将 3 个 int 属性转换为颜色。 然后我有一个 ViewModel,有 2 个道具 1 是这个颜色类,一个是颜色(结果颜色)这个属性是我希望我的矩形从中获取颜色的属性。
主代码:
public ColorModel Col;
public MainWindow()
{
InitializeComponent();
Col = new ColorModel();
var VM = new VM();
VM.Color = Col;
this.DataContext = VM;
}
如果有帮助,这里是 ViewModel。
public class VM : INotifyPropertyChanged
{
private ColorModel _Color { get; set; }
public Color Result { get; set; }
public ColorModel Color
{
get { return _Color; }
set
{
_Color = value;
Result = _Color.GetColor();
_Color.PropertyChanged += _Color_PropertyChanged;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Result"));
}
}
}
private void _Color_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Result = _Color.GetColor();
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Result"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
我真的很感谢每一个答案!我是 WPF 新手。
【问题讨论】:
-
仔细查看错误消息的最后一部分:
target property is 'Fill' (type 'Brush')。画笔不是颜色。 -
请注意,您仅将蓝色滑块正确绑定到
Color.BlueValue,其他绑定缺少Color.部分。 -
还有一点,FromScRgb 不能使用整数值。
标签: c# wpf data-binding colors dependencies