[英]wpf how to update binding on property change of enum in observable collection?
I have a class called SignalViewModel that implements INotifyPropertyChanged, and I am binding to the property in my xaml in the class SignalGraph, but the change is not propagating. 我有一个名为SignalViewModel的类,它实现了INotifyPropertyChanged,我绑定到了SignalGraph类中我的xaml中的属性,但是这个更改没有传播。 The CLR property that I am trying to bind to is called BaseNotation.
我尝试绑定的CLR属性称为BaseNotation。 It is an enum defined as follows:
它的枚举定义如下:
public enum BaseNotation
{
Hex,
SignedDecimal,
UnsignedDecimal,
SignaedBinary,
UnsignedBinary
}
the class and relevant source property 类和相关的源属性
class SignalViewModel : INotifyPropertyChanged
{
private BaseNotation _BaseRepresentation = BaseNotation.Hex;
public BaseNotation BaseRepresentation
{
get
{
return _BaseRepresentation;
}
set
{
if (value != _BaseRepresentation)
{
_BaseRepresentation = (BaseNotation)value;
OnPropertyChanged("BaseRepresentation");
}
}
}
the target property is: 目标属性是:
public BaseNotation BaseRepresentation
{
get
{
return (BaseNotation)GetValue(BaseRepresentationProperty);
}
set
{
SetValue(BaseRepresentationProperty, value);
}
}
public static readonly DependencyProperty BaseRepresentationProperty =
DependencyProperty.Register("BaseRepresentation",
typeof(BaseNotation), typeof(SignalGraph),
new FrameworkPropertyMetadata(BaseNotation.Hex, new PropertyChangedCallback(ReDraw)));
The Binding is to an object in treeview's itemtemplate. Binding是树视图的itemtemplate中的对象。
>
<!--Defines panel used by treeview to place items in itemspresenter-->
<TreeView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</TreeView.ItemsPanel>
<!--Template Defining the layout of items in this treeview-->
<TreeView.ItemTemplate >
<HierarchicalDataTemplate ItemsSource ="{Binding Path = bits}">
<Components:SignalGraph
x:Name="signal_graph"
IsSignal="True"
BaseRepresentation="{Binding Path=BaseRepresentation, Mode=TwoWay}"
PenWidth="{Binding ElementName=graph_viewer, Path=GraphPenWidth, Mode=OneWay}"
BusTextColor="{Binding ElementName=graph_viewer, Path=BusTextPenColor, Mode=TwoWay}"
HighValuePenColor="{Binding ElementName=graph_viewer, Path=HighValuePenColor, Mode=TwoWay}"
LowValuePenColor="{Binding ElementName=graph_viewer, Path=LowValuePenColor, Mode=TwoWay}"
UnknownValuePenColor="{Binding ElementName=graph_viewer, Path=UnknownValuePenColor, Mode=TwoWay}"
Height="{Binding ElementName=graph_viewer, Path=GraphHeight, Mode=OneWay}"
VerticalAlignment="Stretch"
Signal="{Binding}"
MaxTimeValue="{Binding ElementName=graph_viewer, Path = _SignalDataViewModel.MaxTimeValue}"
AxisDivisionUnit="{Binding ElementName=graph_viewer, Path = AxisDivisionUnit}"
MinimumXInDIPs="{Binding ElementName=signal_scrollviewer, Path=HorizontalOffset}"
ViewportWidth="{Binding ElementName=signal_scrollviewer, Path=ViewportWidth}"
/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Components:SignalGraph
x:Name="bit_graph"
IsSignal="False"
Height="{Binding ElementName=graph_viewer, Path=GraphHeight, Mode=OneWay}"
VerticalAlignment="Stretch"
BusTextColor="{Binding ElementName=graph_viewer, Path=BusTextPenColor, Mode=TwoWay}"
HighValuePenColor="{Binding ElementName=graph_viewer, Path=HighValuePenColor, Mode=TwoWay}"
LowValuePenColor="{Binding ElementName=graph_viewer, Path=LowValuePenColor, Mode=TwoWay}"
UnknownValuePenColor="{Binding ElementName=graph_viewer, Path=UnknownValuePenColor, Mode=TwoWay}"
PenWidth="{Binding ElementName=graph_viewer, Path=GraphPenWidth, Mode=OneWay}"
Bit="{Binding}"
MaxTimeValue="{Binding RelativeSource = {RelativeSource AncestorType={x:Type DaedalusGraphViewer:GraphViewer}}, Path = _SignalDataViewModel.MaxTimeValue}"
AxisDivisionUnit="{Binding ElementName=graph_viewer, Path = AxisDivisionUnit}"
MinimumXInDIPs="{Binding ElementName=signal_scrollviewer, Path=HorizontalOffset}"
ViewportWidth="{Binding ElementName=signal_scrollviewer, Path=ViewportWidth}"
/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
I have checked the datacontext of signalgraph in the debugger and it is correctly set to signalviewmodel. 我已经在调试器中检查了signalgraph的datacontext,并将其正确设置为signalviewmodel。 My output does not have any binding errors.
我的输出没有任何绑定错误。 In addition, if I put a break point in the SignalViewModel Class I can see that the OnPropertyChanged Event is being raised.
另外,如果我在SignalViewModel类中放置一个断点,我可以看到正在引发OnPropertyChanged事件。
However, there is no change to the target property when I check it. 但是,检查时,目标属性没有变化。 When I break inside of signalgraph on scrolling, I can check and see that the datacontext's (signalviewmodel's) BaseRepresentation has changed to BaseNotation.UnsignedDecimal.
当我在滚动时打破信号图内部时,我可以检查并看到datacontext的(signalviewmodel的)BaseRepresentation已更改为BaseNotation.UnsignedDecimal。 However, the dependency property BaseRepresentation in SignalGraph has not updated to the new value.
但是,SignalGraph中的依赖项属性BaseRepresentation尚未更新为新值。 Why isn't the binding working?
为什么绑定不起作用?
Try 尝试
{Binding Path=BaseRepresentation, Mode=TwoWay}
More on data binding modes: http://msdn.microsoft.com/en-us/library/ms752347.aspx (Read "direction of the data flow"). 有关数据绑定模式的更多信息: http : //msdn.microsoft.com/en-us/library/ms752347.aspx (阅读“数据流的方向”)。
dang it. 该死的。 I can't believe it was such a simple error.
我无法相信这是一个如此简单的错误。 i've done this before too without thinking but I had forgotten about it because it had been a while.
我之前也没有想过就完成了这个,但我已经忘记了它,因为它已经有一段时间了。 Never ever ever manually set the property to value in the constructor or elsewhere or you break the binding
永远不要在构造函数或其他地方手动将属性设置为值,否则会破坏绑定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.