[英]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_signals
从List<T>
更改为ObservableCollection<T>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.