我必须在我的XAML文件中创建一个静态资源.
<Window.Resources>
<vm:ViewModel x:Key="viewModel" />
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
我需要这个静态资源来获取我的组合框的项目
ItemsSource="{Binding Source={StaticResource viewModel}, Path=GetItems, Mode=TwoWay}"
Run Code Online (Sandbox Code Playgroud)
但是,如何为ViewModel(构造函数)提供我的代码实例?
为了实现我的应用程序,我使用了很多Blend3.当Blend3想要将资源链接到另一个资源时,它会多次使用链接类型"DynamicResource".正如我所理解的那样(但我可能已经理解得不好),只有当我想在运行时修改链接时,"动态"链接才有意义.在其他情况下,他们使用更多的内存是徒劳的.我不想在运行时修改任何东西,那么问题是:有意义在我的所有应用程序中用"StaticResource"替换"DynamicResource"吗?谢谢!Pileggi
wpf performance dynamicresource expression-blend staticresource
我得到这个异常:
'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '13' and line position '6'.
当我尝试运行以下XAML时:
<Window x:Class="WPF_Application.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:me="clr-namespace:WPF_Application"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="me:ExampleBusinessObject" x:Key="BusinessObjectTemplate">
<StackPanel>
<Label HorizontalAlignment="Center" Content="{Binding Path=Title}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<ItemsControl ItemsSource="{Binding BusinessObjects}" ItemTemplate="{StaticResource BusinessObjectTemplate}"/>
</Window>
Run Code Online (Sandbox Code Playgroud)
据我所知,这通常是由静态资源扩展指向不可用的原因引起的,但据我所知,模板应该在那时可用。
我有一个用户控件,资源如下:
<UserControl.Resources>
<Image x:Key="IconPrinter" Source="/PrintViewerWPF;component/Resources/Images/printer.png" />
<MenuItem x:Key="PrintDoc"
Header="Print"
Click="PrintDoc_OnClick"
Icon="{StaticResource IconPrinter}" />
<ContextMenu x:Key="MergedContextMenu">
<StaticResource ResourceKey="PrintDoc"/>
</ContextMenu>
<ContextMenu x:Key="SingleContextMenu">
<StaticResource ResourceKey="PrintDoc"/>
</ContextMenu>
<DataTemplate x:Key="mergedDocDisplayGrid">
<StackPanel StackPanel.ContextMenu="{DynamicResource MergedContextMenu}" />
</DataTemplate>
<DataTemplate x:Key="singleDocDisplayGrid">
<StackPanel StackPanel.ContextMenu="{DynamicResource SingleContextMenu}" />
</DataTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
所述DataTemplate
s的一个的DevExpress内使用FlowLayoutControl
在我的场景中,我有两个项目FlowLayoutControl
.一个使用模板mergedDocDisplayGrid
,另一个使用singleDocDisplayGrid
数据在模板中正确显示,并且已成功实现其他功能,如鼠标活动和拖放
在任一项上显示上下文菜单都有效,但如果我随后尝试再次在另一项上显示上下文菜单,则会出现以下异常:
Message - Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.
Inner - Element already has a logical parent. It must be detached from the old parent before it …
Run Code Online (Sandbox Code Playgroud)