0

我有一个名为 SignalViewModel 的类,它实现了 INotifyPropertyChanged,并且我在 SignalGraph 类中绑定到我的 xaml 中的属性,但更改没有传播。我尝试绑定的 CLR 属性称为 BaseNotation。它是一个枚举,定义如下:

  public enum BaseNotation
  {
    Hex,
    SignedDecimal,
    UnsignedDecimal,
    SignaedBinary,
    UnsignedBinary
  }

类和相关的源属性

class SignalViewModel : INotifyPropertyChanged
{
    private BaseNotation _BaseRepresentation = BaseNotation.Hex;
        public BaseNotation BaseRepresentation
        {
          get
          {
            return _BaseRepresentation;
          }
          set
          {
            if (value != _BaseRepresentation)
            {
              _BaseRepresentation = (BaseNotation)value;
              OnPropertyChanged("BaseRepresentation");
            }
          }
        }

目标属性是:

 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)));

绑定是树视图的项目模板中的一个对象。

        >
        <!--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>

我已经在调试器中检查了信号图的数据上下文,它被正确设置为信号视图模型。我的输出没有任何绑定错误。此外,如果我在 SignalViewModel 类中设置断点,我可以看到 OnPropertyChanged 事件正在引发。

但是,当我检查它时,目标属性没有变化。当我在滚动时打破信号图内部时,我可以检查并看到数据上下文的(信号视图模型的)BaseRepresentation 已更改为 BaseNotation.UnsignedDecimal。但是,SignalGraph 中的依赖属性 BaseRepresentation 尚未更新为新值。为什么绑定不起作用?

4

2 回答 2

2

尝试

{Binding Path=BaseRepresentation, Mode=TwoWay}

有关数据绑定模式的更多信息:http: //msdn.microsoft.com/en-us/library/ms752347.aspx(阅读“数据流方向”)。

于 2013-09-10T01:10:13.187 回答
0

该死的。我不敢相信这是一个如此简单的错误。我以前也这样做过,但我已经忘记了,因为已经有一段时间了。永远不要在构造函数或其他地方手动将属性设置为值,否则你会破坏绑定

于 2013-09-10T19:54:41.717 回答