James Joshua Street
James Joshua Street

Reputation: 3419

binding to dynamic resource in ItemsControl on the ItemsSource Property

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>

Upvotes: 0

Views: 2285

Answers (1)

Fede
Fede

Reputation: 44048

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>

Upvotes: 1

Related Questions