标签: resourcedictionary

将集合或数组添加到wpf资源字典中

我搜索高低,无法找到答案.我有两个问题

  1. 如何在XAML中创建数组或集合.我有一个数组,我想坚持在那里并绑定到一个组合框.我的第一个想法是将ItemsControl放在资源字典中,但组合框的ItemsSource需要IEnumerable,因此不起作用.

这是我在资源字典中尝试过的,但都不起作用

<ItemsControl x:Key="stateList">
    <sys:String>AL</sys:String>
    <sys:String>CA</sys:String>
    <sys:String>CN</sys:String>
</ItemsControl>
<ItemsControl x:Key="stateList2">
    <ComboBoxItem>AL</ComboBoxItem>
    <ComboBoxItem>CA</ComboBoxItem>
    <ComboBoxItem>CN</ComboBoxItem>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

这就是我如何绑定它

<ComboBox SelectedValue="{Binding Path=State}" ItemsSource="{Binding Source={StaticResource stateList2}}"  >

                    </ComboBox>
Run Code Online (Sandbox Code Playgroud)

编辑:更新

我得到了第一部分以这种方式工作

 <col:ArrayList x:Key="stateList3">
    <sys:String>AL</sys:String>
    <sys:String>CA</sys:String>
    <sys:String>CN</sys:String>
</col:ArrayList>
Run Code Online (Sandbox Code Playgroud)

但是,我宁愿不使用数组列表,我想使用通用列表,所以如果有人知道如何请让我知道.

编辑更新:我猜XAML对泛型的支持非常有限,所以也许数组列表是我现在能做的最好的,但是我仍然希望第二个问题的帮助,如果有人有一个anser

第2位.我尝试在我的XAML中引用合并的资源字典并遇到问题,因为在Window.resources下我不仅仅是字典,所以它要求我添加x:Key.添加密钥后,系统将无法再找到资源字典中的项目.我不得不将合并的字典移动到Grid.Resources.理想情况下,我想在app.xaml中引用合并的字典,但我有同样的问题

这是一些示例代码.第一部分需要一个x:key来编译,因为我有转换器并且它抱怨每个项目必须有一个键,如果有多个

<UserControl.Resources>
    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ResourcesD.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

我不得不改变它

<UI:BaseStep.Resources>
    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />             
</UI:BaseStep.Resources>
<Grid>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ResourcesD.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
</Grid>
Run Code Online (Sandbox Code Playgroud)

谢谢

wpf resourcedictionary

8
推荐指数
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
查看次数

如何在ResourceDictionary中使用转换器

当我在Window上将它用作StaticResource时,我有一个很好用的转换器,如下所示

<UserControl.Resources>
           <local:ValidationErrorConverter x:Key="validationErrorConverter"/>       
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

我有一个ResourceDictionary来定义我的控件ControlTemplates和Styles,我无法弄清楚在哪里引用我的转换器作为StaticResource,以便能够在我的样式上使用它如下

<Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding 
RelativeSource={RelativeSource Self}, 
Path=(Validation.Errors).CurrentItem, 
Converter={StaticResource HERE??}}"/>
        </Trigger>
    </Style.Triggers>
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml resourcedictionary staticresource

8
推荐指数
1
解决办法
9432
查看次数

为什么ResourceDictionary中的ControlTemplate需要x:key

考虑以下XAML文件:

<Window x:Class="ExpressionVisualizer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sle="clr-namespace:System.Linq.Expressions;assembly=System.Core"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type sle:BinaryExpression}"/>
        <ControlTemplate TargetType="{x:Type ContentControl}"/>
    </Window.Resources>
</Window>
Run Code Online (Sandbox Code Playgroud)

这给出了以下编译错误:

添加到IDictionary的所有对象必须具有Key属性或与其关联的其他类型的键.第10行位置10.

如果我x:key向ControlTemplate 添加一个属性,它会编译.但是,我不应该这样做.ControlTemplate使用DictionaryKeyProperty属性进行修饰,该属性将TargetType指定为键属性.因此,只要我为ControlTemplate指定TargetType,不必指定显式键(类似于我没有在我定义的DataTemplate上指定一个键).

我有第二个与切向相关的问题.如果我在XAML中以这种方式定义ControlTemplate(或者是否指定键),它是否会自动应用于未指定其他模板的ContentControl类型的所有控件,或者我是否必须将ControlTemplate嵌入到样式中要发生这种情况?

wpf xaml resourcedictionary controltemplate

8
推荐指数
1
解决办法
5304
查看次数

WPF:格式化标签

我有一个扩展器的代码如下:

   <Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
              FontSize="18" FontFamily="Calibri" FontWeight="Bold">
        <StackPanel>
            <Label Content="{StaticResource companyLinksItemSummary}" 
                   FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
            <Label Content="{StaticResource companyLinksItemInfo}" 
                   FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
            <Label Content="{StaticResource companyLinksItemIssues}" 
                   FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
            <Label Content="{StaticResource companyLinksItemMessages}" 
                   FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
        </StackPanel>   
    </Expander>
Run Code Online (Sandbox Code Playgroud)

StaticResources定义如下(在我的资源字典中):

<sys:String x:Key="companyLinksHeader">company</sys:String>
<sys:String x:Key="companyLinksItemSummary">summary</sys:String>
<sys:String x:Key="companyLinksItemInfo">info</sys:String>
<sys:String x:Key="companyLinksItemIssues">issues</sys:String>
<sys:String x:Key="companyLinksItemMessages">messages</sys:String>
Run Code Online (Sandbox Code Playgroud)

有没有办法定义一个字典条目(或其他东西)来处理标题和标签的字体样式,这样我就不必一遍又一遍地定义相同的字体(并且只在一个地方更改它应该我想要改变字体)?

编辑

我找到了一个解决方案(感谢那些发布的解决方案)并且我正在使用以下样式作为StackPanel标签项:

<!-- Expander Items text style -->
<Style x:Key="expanderItemsTextStyle">
    <Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
    <Setter Property="Label.FontWeight" Value="Normal"></Setter>
    <Setter Property="Label.FontSize" Value="14"></Setter>
    <Setter Property="Label.Foreground" Value="Aqua"></Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

并像这样实现它(将它应用于StackPanel,因此它影响所有标签):

<Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
          Style="{StaticResource expanderHeaderTextStyle}">
    <StackPanel Style="{StaticResource …
Run Code Online (Sandbox Code Playgroud)

wpf fonts resourcedictionary

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

扩展/覆盖资源字典中的样式

我在WPF应用程序的单独dll中有一个BaseSkin和多个UserSkins.

根据使用应用程序的用户,基础外观和其中一个用户外观将合并到资源字典中并加载以供应用程序使用.

我的目标是能够在BaseSkin文件中指定样式,然后在特定的UserSkin文件上能够覆盖它,更改我需要的任何属性.

我知道我可以通过使用这样的BasedOn属性来实现这一点:

基础:

<Style x:Key="ButtonBg" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Green"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

用户:

<Style x:Key="CustomButtonBg" TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonBg}">
    <Setter Property="Background" Value="Blue"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

问题是现在元素必须具有StyleButtonBg样式,实际上可能无法实现.有没有办法让两个样式使用相同的键(ButtonBg),并且当它们合并时,应用程序在User用户中查找名为ButtonBg的样式,如果不存在,请使用base中的样式?

我想如果我可以将BasedOn属性中的程序集名称指向BaseSkin文件,当我给它们相同的密钥时,我可以避免命名错误,但我找不到任何方法来做到这一点.其他选项只是强制执行每个样式,即使没有任何变化,或者在皮肤中以编程方式检查,但这些是最后的度假胜地.

c# wpf xaml resourcedictionary

7
推荐指数
1
解决办法
8325
查看次数

我可以向Style添加Resources或ResourceDictionary吗?

是否可以在样式中定义ResourceDictionary?

例如,假设我想为StackPanels设置两种不同的样式,并且在一种情况下,我希望所有按钮都是蓝色,另一种我希望它们是红色.这可能吗?

就像是

<Style x:Key="RedButtonsPanel" TargetType="{x:Type StackPanel}">
    <Setter Property="Orientation" Value="Horizontal" />
    <Setter Property="StackPanel.Resources">
        <Setter.Value>
            <ResourceDictionary>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="Red" />
                </Style>
            </ResourceDictionary>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

上面的代码失败了,关于Setter的Property值的错误不能为null(即使它显然不是null).

我可以做点什么

<ResourceDictionary x:Key="RedButtons">
    <Style TargetType="{x:Type Button}">
        <Setter Property="Width" Value="100" />
        <Setter Property="Background" Value="Red" />
    </Style>
</ResourceDictionary>

<StackPanel Resources={StaticResource RedButtons} />
Run Code Online (Sandbox Code Playgroud)

但是我想知道是否有办法将ResourceDictionary合并到样式中.

wpf xaml styles resourcedictionary

7
推荐指数
2
解决办法
2464
查看次数

WPF:DLL中等效的应用程序资源

我的解决方案中有两个项目.第一个项目是WPF应用程序,另一个是常规DLL项目.在DLL项目中,我有一些WPF用户控件.我希望这些控件共享一些资源并在DLL中定义它们.

我知道在常规WPF应用程序中,您可以在App.xaml中指定应用程序资源.DLL项目中是否有等价物?

wpf wpf-controls resourcedictionary c#-4.0

7
推荐指数
2
解决办法
8219
查看次数

Silverlight 4 - 在另一个ResourceDictionary中使用StaticResource

如果我有这些词典:

dict1.xaml: <Color x:Key="Color1">Red</Color>

dict2.xaml: <SolidColorBrush x:Key="Brush1" Color={StaticResource Color1} />

这有效:

App.xaml中:

<MergedDictionaries>
  <ResourceDictionary Source="dict1.xaml"/>
<MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)

SomePage.xaml:

<UserControl.Resources>
  <MergedDictionaries>
    <ResourceDictionary Source="dict2.xaml"/>
  </MergedDictionaries>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

这不是:

App.xaml在应用程序级别合并.

我收到有关未找到Color1的错误.

为什么是这样 ?/它有办法解决吗?我知道这个例子很简单,但真正的例子太长了.基本上我只是想在不同的文件中组织我的样式和模板:

  • 一个颜色
  • 一个用于隐式样式
  • 许多用于显式样式

编辑:奇怪的是,如果我在代码中执行此操作Application_Startup,在设置RootVisual属性之前,我没有收到错误...我只是为什么感到困惑!

silverlight resourcedictionary staticresource

7
推荐指数
1
解决办法
8022
查看次数

WPF控件作为资源字典中的StaticResource,用于多个WPF Windows?

我有一个Button控件作为资源字典中的资源,如下所示:

<!--ButtonResources.xaml file-->
<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Button x:Key="buttonResource" Content={Binding BoundText}/>
</ResourceDictionary>
<!--ButtonResources.xaml file-->
Run Code Online (Sandbox Code Playgroud)

我现在用这以上按钮控制绑定到ContentControl中控制的内容属性2个不同的视窗的.xaml文件,其中每个Window都有自己的DataContext,从而每一个窗口应该显示Content基于其上述按钮控制的ViewModel's BoundText属性值,如下对每个窗口.

<Window x:Class="TestClass1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ButtonResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <ContentControl Content={StaticResource buttonResource}/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

但是,问题是两个Window都显示了BoundText属性的相同值,这意味着两个WPF Windows都具有来自资源的相同按钮控制实例,在Windows中都使用.

如何解决此问题,以便每个窗口从资源获取单独的按钮控件,并仍然从他们自己的ViewModel显示属性的不同值?BoundText

编辑: 由于下面提到的原因MSDN,我不能使用x:Shared ="False"属性来解决此问题:

•包含项的ResourceDictionary不得嵌套在另一个ResourceDictionary中.例如,对于已经是ResourceDictionary项的Style内的ResourceDictionary中的项,您不能使用x:Shared.

wpf xaml .net-4.0 resourcedictionary staticresource

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