【问题标题】:binding to dynamic resource in ItemsControl on the ItemsSource Property在 ItemsSource 属性上绑定到 ItemsControl 中的动态资源
【发布时间】:2013-06-07 19:32:02
【问题描述】:

嘿,我希望有人可以为我回答几个问题。我应该如何确保绑定到 itemsource 的数据动态更新?我无法将 bindingsource 从静态资源更改为动态资源,因为 Object Binding 的 Source 属性不是依赖对象的依赖属性。

绑定到静态资源到底是什么意思?我认为绑定到动态资源意味着依赖属性会在资源更改时更新。

绑定到静态资源是否只是附加资源的初始值?

我的目标只是根据 signal_data 更新 signal_viewer。

<UserControl.Resources>
    <wpfExp:SignalData x:Key="signal_data" />
</UserControl.Resources>

<DockPanel x:Name ="maindockpanel"  Height ="Auto" Width ="Auto" LastChildFill="True">
  <ToolBarTray DockPanel.Dock="Top">
    <ToolBar HorizontalAlignment="Stretch" VerticalAlignment="Top">
      <Button Name="load_button" Height="20" Width="Auto" Click="Load_Button_Click">Load</Button>
      <Button Name="zoom_in_button" Click="zoom_in_button_Click">Zoom In</Button>
      <Button Name="zoom_out_button" Click="zoom_out_button_Click">Zoom Out</Button>
    </ToolBar>
  </ToolBarTray>

  <ItemsControl x:Name ="Signalviewer_Control" ItemsSource="{Binding Source = {StaticResource signal_data}, Path = list_of_signals}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <wpfExp:SignalViewer Signal="{Binding}" MainText="{Binding Path = SignalName}"/>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>

【问题讨论】:

    标签: c# wpf xaml itemscontrol


    【解决方案1】:

    由于您提到的所有这些问题,我完全反对将 ViewModel 或数据作为 Resource 在 XAML 中。

    相反,在后面的代码中分配DataContext

    public SomeWindow() //Window Constructor
    {
        DataContext = new SomeViewModel();
    }
    

    或使用ViewModelLocator

    或使用here 概述的RegisterDataTemplate 方法。


    无论如何,如果您想快速解决此问题,请将您的 list_of_signalsList&lt;T&gt; 更改为 ObservableCollection&lt;T&gt;

    【讨论】:

    • 啊,我读到过,但我不确定是否需要,因为我只希望在给出新 List 时更新属性,而不是在列表中的任何信号发生变化时更新.无论如何感谢您的建议。我将阅读您提到的两种方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多