我DataTemplate在xaml文件中定义了我想通过C#代码访问的文件.任何人都可以告诉我如何访问它?我添加了一个新ResourceDictionary文件,它的名字是Dictionary1.xaml.我有一个数据模板,如:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="mytemplate">
<TextBlock Text="Name:" Background="Blue"/>
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
不是我有一个ListBox名为listBox1,我想将它分配给它的Itemtemplate属性,但我不知道怎么办呢?
我目前正在尝试实现Metro风格的窗口.
所以我在ResourceDictionary中创建了以下样式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Brushes -->
<SolidColorBrush x:Key="BackgroundColor" Color="#FFFFFFFF" />
<!-- Buttons -->
<Style x:Key="MetroControlBoxButton" TargetType="Button">
<Setter Property="Background" Value="{StaticResource BackgroundColor}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Windows -->
<Style x:Key="MetroWindow" TargetType="Window">
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<Grid Background="{StaticResource BackgroundColor}">
<Grid.RowDefinitions>
<RowDefinition Height="6" />
<RowDefinition Height="24" />
<RowDefinition Height="*" />
<RowDefinition Height="24" />
<RowDefinition Height="6" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6" …Run Code Online (Sandbox Code Playgroud) 我创建了一个类库,它包含WPF Windows和一些从我的c#类继承的用户控件,可以帮助我自定义某些wpf控件.
现在我想添加ResourceDictionary,以帮助我在我的wpf类之间共享样式.可能吗?
谢谢.
编辑:位于MY.WpfPresentation.Main项目中的资源字典文件(名为Styles.xaml):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
xmlns:MYNetMisc="clr-namespace:MY.Net.Misc;assembly=MY.Net"
>
<Style x:Key="customRowStyle" BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=RowStyle}}" TargetType="{x:Type dxg:GridRowContent}">
<Setter Property="Foreground" Value="{Binding Path=DataContext.balance, Converter={MYNetMisc:BalanceToColor OnlyNegative=false}}" />
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
使用它:
<MYNetPresentation:frmDockBase.Resources>
<ResourceDictionary x:Key="style">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MY.WpfPresentation.Main;component/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<DataTemplate x:Key="TabTemplate">
<dxlc:LayoutControl Padding="0" ScrollBars="None" Background="Transparent">
<Image Source="/Images/Icons/table-32x32.png" Width="12" Height="12" />
<TextBlock Text="{Binding}" HorizontalAlignment="Left" VerticalAlignment="Center" />
</dxlc:LayoutControl>
</DataTemplate>
</MYNetPresentation:frmDockBase.Resources>
Run Code Online (Sandbox Code Playgroud) 我有一个资源字典:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfUI2.MainWindowEvents">
<DataTemplate
x:Key="WorkspacesTemplate">
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"/>
</DataTemplate>
...
Run Code Online (Sandbox Code Playgroud)
我想为TabControl添加一个事件处理程序.MainWindowEvents是在没有其他类的文件中定义的类:
Namespace wpfUI2
Public Class MainWindowEvents
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
当我去添加一个事件处理程序时
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
SelectionChanged=""
/>
Run Code Online (Sandbox Code Playgroud)
并尝试在""之间单击以创建事件我收到一条错误,指出x:Class属性指定的类必须是文件中的第一个.好吧!奇怪的是,当我手动创建处理程序时:
Namespace wpfUI2
Public Class MainWindowEvents
Public Sub Tab1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
End Sub
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
一切都编译好,但我在window.show上得到一个运行时异常
我究竟做错了什么?
我需要在资源字典后面添加代码,如本问题所述.(我知道这不是一个好习惯,但它应该基于链接问题的评论工作.)我用x:Class属性引用代码:
XAML(单独的资源字典文件):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyNamespace.MyStandardResources">
...
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
码:
using System.Windows;
namespace MyNamespace
{
public partial class MyStandardResources : ResourceDictionary
{
public MyStandardResources()
{
InitializeComponent();
}
//...
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致运行时解析器异常:
分析器内部错误:对象编写器 ' xClassNotDerivedFromElement '.System.Windows.Application.LoadComponent中的[Line:xxx Position:xxx].
资源包含在带有ResourceDictionary.MergedDictionaries标记的App.xaml中.
我有一个ContentControl,其内容由DataTemplateSelector基于属性Workspace确定.但是当数据模板被更改时,我必须根据ContentControl的初始大小和整个Window进行一些计算,所以我想知道它何时被加载.
<ContentControl Content="{Binding Path=Workspace}" ContentTemplateSelector="{StaticResource workspaceTemplateSelector}" />
Run Code Online (Sandbox Code Playgroud)
ResourceDictionary中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="clr-namespace:Capgemini.Sag.KeyEm.View">
<DataTemplate x:Key="keyboardTemplate" >
<vw:Keyboard/>
</DataTemplate>
<DataTemplate x:Key="welcomeTemplate">
<vw:Welcome/>
</DataTemplate>
<vw:WorkspaceTemplateSelector
KeyboardTemplate="{StaticResource keyboardTemplate}"
WelcomeTemplate="{StaticResource welcomeTemplate}"
x:Key="workspaceTemplateSelector"/>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
DataTemplateSelector:
using Capgemini.Sag.KeyEm.ViewModel.Interfaces;
namespace Capgemini.Sag.KeyEm.View
{
using System.Windows;
using System.Windows.Controls;
class WorkspaceTemplateSelector : DataTemplateSelector
{
public DataTemplate WelcomeTemplate { get; set; }
public DataTemplate KeyboardTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is IWelcomeViewModel)
return WelcomeTemplate;
if (item is IKeyboardViewModel)
return KeyboardTemplate;
return null;
} …Run Code Online (Sandbox Code Playgroud) 有没有办法获得特定WPF资源的价值变化的通知?
我们需要在WPF应用程序中动态调整内容字体大小...对于WPF控件,我们设置Control.FontSize为动态资源,字体自动调整大小.不幸的是,我们还有一个嵌入式winforms控件,其字体大小不能这样设置.我们的想法是订阅在每个资源值更改时触发的事件,并实现winforms控件的自定义刷新.有什么建议吗?
先感谢您!
我有这个xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:My.Windows"
>
<ObjectDataProvider x:Key="TitledWindow_Test" MethodName="Test" ObjectInstance={x:Type l:TitledWindow}">
<ControlTemplate x:Key="TitledWindowControlTemplateKey" x:Name="PART_ControlTemplate" TargetType="{x:Type l:TitledWindow}"
<Rectangle>
<Rectangle.Style>
<EventSetter Event="Mouse.MouseEnter" Handler="{StaticResource TitledWindow_Test}">
</Rectangle.Style>
</Rectangle>
</ControlTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
我的c#代码:
namespace My.Windows
{
public partial class TitledWindow : Window
{
public void Test()
{
MessageBox.Show("Test");
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我收到以下错误:
错误1
"ResourceDictionary"根元素需要ax:Class属性来支持XAML文件中的事件处理程序.删除MouseEnter事件的事件处理程序,或将ax:Class属性添加到根元素.
我发现了这篇关于提高ResourceDictionaries网络性能的文章,但问题是它已经很旧了(2011 年)。我正在考虑实现这样的东西,但我想知道 Microsoft 是否没有在 .NET Framework 的最新版本中修复此问题。关于这个话题我有几个问题,希望这里有人能给出答案:
ResourceDictionaries通过创建它们的多个实例(每次将它们分配给控件时一个实例)来进行管理?"CustomButtonStyle"例如,当我在ResourceDictionary名为 的"ButtonStyles"程序集中调用样式,然后在应用程序中"StylesAssembly"引用该样式,并为其分配了 20 个时,这甚至是一个问题吗?App.xamlButtonsCustomButtonStyle"ButtonStyles" ResourceDictionary?wpf ×8
c# ×6
xaml ×4
binding ×2
events ×2
.net ×1
code-behind ×1
command ×1
datatemplate ×1
resources ×1
silverlight ×1
winforms ×1
wpf-controls ×1