【发布时间】:2016-01-07 09:09:09
【问题描述】:
我正在使用 observable 集合和 dataTemplates 创建通用面板,当我的按钮颜色(以编程方式设置)初始化时遇到问题,但一旦在运行时更改颜色保持不动。
这是我的按钮的 Xaml 代码:
<Button Grid.Column="0" Grid.Row="3" Background="{Binding Path=motionColor}" Command="{Binding Path=MotionPress}" Content="Motion" Height="51" Width="104" Style="{StaticResource MyButtonStyle}"/>
这是我的数据模板:
<DataTemplate x:Key="GenericPanelTemplate" DataType="local:GenericPanel">
这是 GenericPanel.cs 中的相关后端代码:
运动颜色:
public Brush motionColor { get; set; }
按钮最终执行的功能(测试并运行):
public void MotionButton_Click()
{
motionColor = setColor((byte)Value.ON);
}
private Brush setColor(byte value)
{
switch (value)
{
case (byte)Value.OFF:
return Brushes.Red;
case (byte)Value.ON:
return Brushes.Green;
case (byte)Value.PROGRESS:
return Brushes.Yellow;
default:
return Brushes.Gray;
}
}
启动时,motionColor设置为红色,按钮确实显示为红色,但是当这里的值更改为Brushes.Green时,它不再起作用(调试证明该值更改正确)。
如何使按钮检查其绑定值并更新背景属性?
如果需要更多代码请询问,我认为这些函数和 Xaml 行是唯一相关的。
感谢您的帮助。
【问题讨论】:
-
您的
GenericPanel必须实现INotifyPropertyChanged 接口或motionColor必须是dependecy property。否则你的绑定永远不会知道有些东西已经改变了......