gpt4 book ai didi

c# - 在 ResourceDictionary 文件中使用 viewbox

转载 作者:行者123 更新时间:2023-11-30 14:32:56 29 4
gpt4 key购买 nike

我有 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>

PhoneAppplicationPage的代码

    <phone:PhoneApplicationPage.Resources>

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceFile1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</phone:PhoneApplicationPage.Resources>

但是这段代码不起作用。设计器错误:InvalidOperationException:元素已经是另一个元素的子元素。还有<Button Content="{StaticResource Flash-On}"/>如果我使用类似的代码运行时错误:无法分配给属性“System.Windows.Controls.ContentControl.Content”。如果在 Grid.Resources 中使用 View 框没有问题,但我想与 ResourceDictionary 一起使用。我该怎么做?

最佳答案

因为 Silverlight resource dictionary 里面的所有东西必须是可共享的。在 WPF 中,您可以使用 x:Shared资源字典中对象的属性,以强制 WPF 为每个资源检索创建一个新实例。要在 Silverlight 中避免这种情况,您可以创建一个 DataTemplate:

<DataTemplate x:Key="ButtonTemplate">
<Viewbox>
<!-- Here your content-->
</Viewbox>
</DataTemplate>

<Button ContentTemplate="{StaticResource ButtonTemplate}"/>

更新 0

我写了一个示例,它根据 CheckBox 的值更改模板。

转换器更改模板:

public class TemplateSelectorConverter : IValueConverter
{
public DataTemplate TrueTemplate { get; set; }

public DataTemplate FalseTemplate { get; set; }

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (((bool) value))
return TrueTemplate;
return FalseTemplate;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}

资源:

<DataTemplate x:Key="FirstTemplate">
<TextBox Text="FirstTemplate" />
</DataTemplate>

<DataTemplate x:Key="SecondTemplate">
<TextBox Text="SecondTemplate" />
</DataTemplate>

<internal:TemplateSelectorConverter x:Key="TemplateSelector" TrueTemplate="{StaticResource FirstTemplate}"
FalseTemplate="{StaticResource SecondTemplate}" />

Xaml 标记:

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<CheckBox Name="CheckBox"/>
<Button Grid.Column="1"
ContentTemplate="{Binding Path=IsChecked, ElementName=CheckBox, Converter={StaticResource TemplateSelector}}"
VerticalAlignment="Top" />
</Grid>

希望对您有所帮助。

关于c# - 在 ResourceDictionary 文件中使用 viewbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17566131/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com