binding to dynamic resource in ItemsControl on the ItemsSource Property

隐身守侯 提交于 2019-12-25 05:32:10

问题


Hey I was hoping someone could answer a couple questions for me. How am I supposed to ensure that the bound data to itemsource updates dynamically? I can't change bindingsource from staticresource to dynamic resource because the Source Property of the Object Binding is not a dependency property of a dependency object.

What does binding to a staticresource mean exactly? I would think that binding to a dynamicresource would mean that the dependencyproperty updates when the resource changes.

Does binding to a static resource merely attach the resource's initial value?

My goal is just to have signal_viewer update based on the signal_data.

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

回答1:


I'm all against putting the ViewModel or data as a Resource in XAML due to all these issues you mentioned.

Instead, either assign the DataContext in code behind:

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

or use ViewModelLocator

or use the RegisterDataTemplate approach outlined here.


Anyways, if you want to resolve this quickly, change your list_of_signals from List<T> to an ObservableCollection<T>



来源:https://stackoverflow.com/questions/16991647/binding-to-dynamic-resource-in-itemscontrol-on-the-itemssource-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!