使用视框在ResourceDictionary中的文件 [英] using viewbox in ResourceDictionary file

查看:118
本文介绍了使用视框在ResourceDictionary中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ResourceFile1.xaml文件
其内容

i have ResourceFile1.xaml file its content

<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代码

The code for PhoneAppplicationPage

    <phone:PhoneApplicationPage.Resources>

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



但这种代码不工作。
设计错误:InvalidOperationException异常:元素已经是另一个元素的孩子。
<按钮内容={StaticResource的闪光灯开启}/> 如果我使用类似的代码
运行时错误:无法分配财产System.Windows.Controls.ContentControl.Content。
如果Grid.Resources视框中使用没有问题,工作正常,但我想与ResourceDictionary.how使用我能做什么?

but this codes not working. Designer Error: InvalidOperationException: Element is already the child of another element. also <Button Content="{StaticResource Flash-On}"/> If I use a similar code Runtime Error: Failed to assign to property 'System.Windows.Controls.ContentControl.Content'. if viewbox use in Grid.Resources no problem works fine but i want to use with ResourceDictionary.how can i do?

推荐答案

由于一个的Silverlight资源字典内的一切必须是共享的。在WPF中,你可以使用 X:共享的对象属性的资源字典里面迫使WPF为每一个资源检索创建一个新的实例。
。要避免这种情况在Silverlight中,你可以创建一个的DataTemplate

Because everything inside a Silverlight resource dictionary must be shareable. In WPF, you can use the x:Shared attribute on objects inside a resource dictionary to force WPF to create a new instance for every resource retrieval. To avoid this in Silverlight you can create a DataTemplate:

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

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



更新0

我写的,其改变依赖于复选框

I wrote an example that changes the template that depends on the value of the CheckBox.

转换器的值设置为模板的例子更改模板:

Converter to change a template:

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();
    }
}



资源:

Resources:

<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标记:

Xaml markup:

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



我希望这有助于。

I hope this helps.

这篇关于使用视框在ResourceDictionary中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆