相关疑难解决方法(0)

如何从C#代码访问wpf中的ResourceDictionary?

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属性,但我不知道怎么办呢?

c# wpf resources

58
推荐指数
3
解决办法
8万
查看次数

用于ControlTemplate的ResourceDictionary中的WPF事件

我目前正在尝试实现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 events xaml command resourcedictionary

25
推荐指数
1
解决办法
1万
查看次数

将ResourceDictionary添加到类库

我创建了一个类库,它包含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)

c# wpf class-library resourcedictionary

23
推荐指数
5
解决办法
4万
查看次数

如何在资源字典中的datatemplate中添加事件处理程序来控制

我有一个资源字典:

<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上得到一个运行时异常

我究竟做错了什么?

wpf datatemplate event-handling resourcedictionary

16
推荐指数
1
解决办法
2万
查看次数

在Silverlight中向Resource Dictionary添加Code Behind时出现xClassNotDerivedFromElement错误

我需要在资源字典后面添加代码,如本问题所述.(我知道这不是一个好习惯,但它应该基于链接问题的评论工作.)我用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中.

.net silverlight xaml code-behind resourcedictionary

8
推荐指数
2
解决办法
2589
查看次数

我如何知道更改时何时加载ContentControl的内容

我有一个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)

c# wpf binding wpf-controls

6
推荐指数
1
解决办法
3042
查看次数

WPF:是否有资源更改触发的事件

有没有办法获得特定WPF资源的价值变化的通知?

我们需要在WPF应用程序中动态调整内容字体大小...对于WPF控件,我们设置Control.FontSize为动态资源,字体自动调整大小.不幸的是,我们还有一个嵌入式winforms控件,其字体大小不能这样设置.我们的想法是订阅在每个资源值更改时触发的事件,并实现winforms控件的自定义刷新.有什么建议吗?

先感谢您!

c# wpf resourcedictionary winforms

6
推荐指数
1
解决办法
1558
查看次数

绑定/引用XAML WPF的方法

我有这个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属性添加到根元素.

c# wpf events xaml binding

5
推荐指数
1
解决办法
9251
查看次数

同一 ResourceDictionary 的多个实例

我发现了这篇关于提高ResourceDictionaries网络性能的文章,但问题是它已经很旧了(2011 年)。我正在考虑实现这样的东西,但我想知道 Microsoft 是否没有在 .NET Framework 的最新版本中修复此问题。关于这个话题我有几个问题,希望这里有人能给出答案:

  1. .NET Framework 4.6.1 是否仍然ResourceDictionaries通过创建它们的多个实例(每次将它们分配给控件时一个实例)来进行管理?
  2. "CustomButtonStyle"例如,当我在ResourceDictionary名为 的"ButtonStyles"程序集中调用样式,然后在应用程序中"StylesAssembly"引用该样式,并为其分配了 20 个时,这甚至是一个问题吗?App.xamlButtonsCustomButtonStyle
  3. 我的理解是否正确,在上面的例子中,将会有 20 个实例"ButtonStyles" ResourceDictionary

c# wpf xaml resourcedictionary

5
推荐指数
1
解决办法
1602
查看次数