绑定到ItemsSource属性上ItemsControl中的动态资源 [英] binding to dynamic resource in ItemsControl on the ItemsSource Property
问题描述
嘿,我希望有人能为我回答几个问题.我应该如何确保绑定到itemsource的数据动态更新?我无法将绑定源从staticresource更改为动态资源,因为Object Binding的Source属性不是依赖项对象的依赖项属性.
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?
我的目标只是基于signal_data更新signal_viewer.
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>
推荐答案
由于您提到的所有这些问题,我都反对将ViewModel或数据作为Resource
放在XAML中.
I'm all against putting the ViewModel or data as a Resource
in XAML due to all these issues you mentioned.
相反,可以在后面的代码中分配DataContext
:
Instead, either assign the DataContext
in code behind:
public SomeWindow() //Window Constructor
{
DataContext = new SomeViewModel();
}
或使用 ViewModelLocator
或使用无论如何,如果要快速解决此问题,请将list_of_signals
从List<T>
更改为ObservableCollection<T>
Anyways, if you want to resolve this quickly, change your list_of_signals
from List<T>
to an ObservableCollection<T>
这篇关于绑定到ItemsSource属性上ItemsControl中的动态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!