简体   繁体   English

绑定到ItemsSource属性上ItemsControl中的动态资源

[英]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? 我应该如何确保绑定到itemsource的数据动态更新? 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. 我无法将绑定源从staticresource更改为动态资源,因为Object Binding的Source属性不是依赖项对象的依赖项属性。

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. 我的目标只是基于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>

I'm all against putting the ViewModel or data as a Resource in XAML due to all these issues you mentioned. 由于您提到的所有这些问题,我都反对将ViewModel或数据作为Resource放入XAML中。

Instead, either assign the DataContext in code behind: 相反,可以在后面的代码中分配DataContext

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

or use ViewModelLocator 或使用ViewModelLocator

or use the RegisterDataTemplate approach outlined here . 或使用此处概述的RegisterDataTemplate方法。


Anyways, if you want to resolve this quickly, change your list_of_signals from List<T> to an ObservableCollection<T> 无论如何,如果要快速解决此问题,请将list_of_signalsList<T>更改为ObservableCollection<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置不带有ItemsControl的Canvas子级属性ItemsItem绑定属性 - Setting Canvas Children Property without ItemsControl ItemsSource Binding Property 将项目的属性从ItemsControl绑定到ItemsSource之外的值? - Binding property of item from ItemsControl to value outside of ItemsSource? ItemsControl不会更新ItemsSource绑定 - ItemsControl does not update the ItemsSource binding ItemsControl.ItemsSource,绑定无效 - ItemsControl.ItemsSource, binding doesn't work ItemsControl中的绑定不会冒泡到ItemsSource中的项目 - Binding in ItemsControl not bubbling to the item from ItemsSource ListView ItemsSource属性绑定 - ListView ItemsSource property binding 如何从ItemsControl中获取除ItemsSource之外的其他类的绑定? - How to get a Binding to other Class than ItemsSource from within ItemsControl? 当ItemsControl也绑定到相同的ItemsSource时,DataGrid排序非常慢 - DataGrid sorting very slow when ItemsControl also binding to the same ItemsSource 将自定义控件中的泛型类型 ItemsControl ItemsSource 绑定到 DependencyProperty - Binding generic type ItemsControl ItemsSource in custom control to DependencyProperty MaterialDesign XAML Transitioner像在ItemsControl中一样绑定ItemsSource和ItemTemplate - MaterialDesign XAML Transitioner Binding ItemsSource and ItemTemplate like in ItemsControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM