我正在尝试为我的用户控件设置样式.UserControl位于项目"控件"中,主题位于项目"MainProject"中
<UserControl x:Class="Controls.OutputPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="OutputControl">
<!-- Style="{DynamicResource UserControlStyle}"> - I cant set the style here because the Resource Dictionary hasn't been defined yet -->
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MainProject;component/Themes/MyTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<!-- Now that the Resource Dictionary has been defined I need to set the style -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="textbox"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Text="{Binding ElementName=OutputControl, Path=TextProperty}"
IsReadOnly="True"
Style="{DynamicResource OutputTextBoxStyle}"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud) 在我的应用程序资源中,我有:
<Application.Resources>
<Border x:Key="border1" BorderBrush="{x:Null}" BorderThickness="0" Height="159" Width="5" >
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFC0C0C" Offset="0" />
<GradientStop Color="#63FF0000" Offset="0.999" />
<GradientStop Color="#6AFE0000" Offset="0.048" />
</LinearGradientBrush>
</Border.Background>
</Border>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
我想将这个边框添加到堆栈面板,如:
Border temp = new Border();
temp = (Border)FindResource("border1");
temp.Name = "bar" + i;
stackPanel1.Children.Add(temp);
Run Code Online (Sandbox Code Playgroud)
这很好用.唯一的问题是我想添加该边框的两个实例.因此我把它放在一个循环中:
for (int i = 0; i < 10; i++)
{
Border temp = new Border();
temp = (Border)FindResource("border1");
temp.Name = "bar" + i;
stackPanel1.Children.Add(temp);
}
Run Code Online (Sandbox Code Playgroud)
在第二次迭代我得到错误:

但对我来说,似乎没有解析例,因为注意第一次迭代没有问题:

我怎么能多次使用资源?我知道我可以动态创建该资源,但我需要实际使用该资源.
我想重用多种UserControl类型的一些样式.
我希望一些Border控件的背景能够闪现,我希望他们都能使用相同的样式,静态资源和动画,这样它们就会同步闪烁.
为此,我在资源字典中定义了一些常用颜色,如下所示:
<SolidColorBrush x:Key="StatusErrorBackground" Color="#440000" />
Run Code Online (Sandbox Code Playgroud)
...我还在这本词典中定义了一个StoryBoard,如下所示:
<Storyboard x:Key="BackgroundAnimation">
<ColorAnimation
Storyboard.Target="{StaticResource StatusErrorBackground}"
Storyboard.TargetProperty="Color"
From="#440000"
To="#ff0000"
Duration="0:0:1"
RepeatBehavior="Forever"
AutoReverse="True"/>
</Storyboard>
Run Code Online (Sandbox Code Playgroud)
然后我将以下内容添加到顶级UserControl:
<FrameworkElement.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CommonResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</FrameworkElement.Resources>
<FrameworkElement.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource BackgroundAnimation}"/>
</EventTrigger>
</FrameworkElement.Triggers>
Run Code Online (Sandbox Code Playgroud)
...然后在其他各种UserControls中,我重新导入ResourceDictionary上面的内容并使用{StaticResource StatusErrorBackground}for a Background.
有问题的元素是红色的(如SolidColorBrush声明中所示),但它们并没有闪烁.
也许这样做不会对有问题的元素引发相应的PropertyChanged通知,所以它们不会被重绘?或类似的东西.该Color物业SolidColorBrush是不是一个依赖项属性,但SolidColorBrush器物IAnimatable,所以很明显有神奇的事情发生在幕后这里后面我不明白.
或者是因为我在两个不同的地方导入相同的资源字典(一次在我的顶级UserControl加一次在我的孩子中)我最终得到两个独立的StaticResource引用?如果您ResourceDictionary在两个不同的控件中导入相同的文件,它是否为每个控件创建独立的资源?在这种情况下,我可以通过在应用程序级别将其拉入来解决这个问题,我猜...
谁能告诉我我做错了什么以及如何解决它?
我需要将应用程序样式分成几个xaml文件.但我还需要定义一些共享值
<x:Double x:Key="SharedValue">100</x:Double>
Run Code Online (Sandbox Code Playgroud)
在单个文件中,在其他文件中定义的样式中使用此值.例如:
<Style x:Name="SomeStyle" TargetType="TextBox">
<Setter Property="Width" Value="{StaticResource SharedValue}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
在另一个资源字典文件中:
<Style x:Name="AnotherStyle" TargetType="Button">
<Setter Property="Height" Value="{StaticResource SharedValue}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在App.xaml文件中定义合并资源字典时
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DefinedValues.xaml"/>
<ResourceDictionary Source="Styles1.xaml"/>
<ResourceDictionary Source="Styles2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
我得到这个运行时异常:"Message ="找不到名称/密钥SharedValue的资源"
你能告诉我这样做是否可行以及我做错了什么?谢谢.
<Application x:Class="CustControls.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ControlLibrary;component/Styles/ControlResource.xaml"/>
<ResourceDictionary Source="StringLocalization/Dictionary_fr-FR.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
现在我想将 ResourceDictionary 的源名称Source="StringLocalization/Dictionary_fr-FR.xaml"更改为 Source="StringLocalization/Dictionary_en-US.xaml"
我该怎么做。
我有ResourceFile1.xaml文件的内容
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Viewbox x:Key="Flash-On" >
<Grid Width="256" Height="256" Visibility="Visible">
<Path Tag="Icon" Data="F1M376.251,632.755L385.665,632.755 381.302,646.07 394.618,646.07 389.11,660.302 393.01,660.302 381.531,672.93 377.398,660.763 381.073,660.763 383.829,652.268 369.825,652.268 376.251,632.755z" Stretch="Uniform" Fill="#FFFFFFFF" Width="176" Height="176" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<TransformGroup>
<TransformGroup.Children>
<RotateTransform Angle="0" />
<ScaleTransform ScaleX="1" ScaleY="1" />
</TransformGroup.Children>
</TransformGroup>
</Path.RenderTransform>
</Path>
</Grid>
</Viewbox>
<Viewbox x:Key="Flash-Off">
....
</Viewbox>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
PhoneAppplicationPage的代码
<phone:PhoneApplicationPage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceFile1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</phone:PhoneApplicationPage.Resources>
Run Code Online (Sandbox Code Playgroud)
但这个代码不起作用.Designer Error:InvalidOperationException:Element已经是另一个元素的子元素.还有<Button Content="{StaticResource Flash-On}"/>如果我使用类似的代码运行时错误:无法分配财产"System.Windows.Controls.ContentControl.Content".如果Viewbox在Grid.Resources中使用没有问题可以正常工作,但我想使用ResourceDictionary.how我可以吗?
感谢您的想法,以帮助相对的初学者了解WPF。
我试图在WAML应用程序的XAML文件中使用以下样式模板:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Style TargetType="{x:Type RowDefinition}" x:Key="hideIfNotDischarged">
<Style.Triggers>
<DataTrigger Binding="{Binding DischargedBy28Days, Mode=OneWay}" Value="false">
<Setter Property="Height" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type RowDefinition}" x:Key="hideIfOutcomeKnownAndAlive">
<Style.Triggers>
<DataTrigger Binding="{Binding IsKnownDead, Mode=OneWay}" Value="false">
<Setter Property="Height" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
稍后将在像这样的网格中使用:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="30" />
<RowDefinition Style="{StaticResource hideIfNotDischarged}" />
...
Run Code Online (Sandbox Code Playgroud)
但是,如果有多个针对Type RowDefinition的样式元素,并且ResourceDictionary嵌套在MergedDictionary中(即使仅合并了一个子ResourceDictionary),则应用程序将失败
System.Windows.ResourceDictionary.DeferrableContent:项目已添加
也就是说,尽管这两种样式具有不同的键,但是资源字典试图添加名称完全基于目标类型的字典项(并忽略键)。
我将如何克服这些问题方面的任何帮助将不胜感激。
我是WPF和XAML的新手,所以我现在有ResourceDictionary一个按钮:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ButtonProduct" TargetType="Button">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border"
CornerRadius="0"
BorderThickness="0"
Focusable="False"
BorderBrush="Transparent" Background="White">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#52b0ca"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在悬停时,按钮的颜色会发生变化,但是如何在淡入和淡出时进行更改,以便平滑过渡颜色?
GridViewColumnHeader继承FrameworkElement,但GridViewColumn不继承。这是否意味着我们无法在资源字典中对后者进行样式设置?
文字片段
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type GridViewColumn}">
</Style>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background" Value="LightBlue"/>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
带有错误消息的代码段

请提前原谅我。第一个问题。
我正在开发一个 WPF 项目,在该项目中我在应用程序级别定义了一个简单的资源字典。
<Application x:Class="Game.UI.Modals.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="GameResources.xaml"></ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
对 ResourceDictionary 的引用工作正常;我可以毫无问题地在 XAML 中使用它。
我遇到的问题是尝试使用 Create ControlTemplate Resource 工具(右键单击 => 编辑模板 => 创建空)将模板添加到字典中。定义面板中的资源字典单选按钮变灰。
我尝试在不同的命名空间中创建字典,我确保字典被命名并且构建操作设置为资源。
知道如何让 Create ControlTemplate Resource 对话框识别我的应用程序级 ResourceDictionary 吗?
谢谢!