相关疑难解决方法(0)

ControlTemplate或DataTemplate中的自定义资源字典

编辑:当使用标准.NET ResourceDictionary时也会出现此问题,并且似乎是在控件或数据模板中使用资源字典的问题.

我有一个自定义资源字典遵循共享资源实例的常见方法.

http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/ http://www.wpftutorial.net/MergedDictionaryPerformance.html

public class SharedResourceDictionary : ResourceDictionary
{
    static readonly Dictionary<Uri, WeakReference<ResourceDictionary>> SharedDictionaries = new Dictionary<Uri, WeakReference<ResourceDictionary>>();

    Uri _sourceUri;

    public new Uri Source
    {
        get
        {
            // Behave like standard resource dictionary for IDE...
            if (VisualStudio.IsInDesignMode)
                return base.Source;

            return this._sourceUri;
        }
        set
        {
            // Behave like standard resource dictionary for IDE...
            if (VisualStudio.IsInDesignMode)
            {
                base.Source = value;
                return;
            }

            this._sourceUri = value;

            WeakReference<ResourceDictionary> cached;
            if (SharedDictionaries.TryGetValue(value, out cached))
            {
                ResourceDictionary rd;
                if (cached.TryGetTarget(out rd))
                {
                    this.MergedDictionaries.Add(rd);
                    return; …
Run Code Online (Sandbox Code Playgroud)

wpf xaml datatemplate resourcedictionary controltemplate

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

Setter.TargetName不适用于BasedOn设置中的元素

我有很多PathGeometry实例,可以为不同的按钮绘制各种图形.所以我创建了一个按钮类型来显示Path,然后根据按钮状态更新Path.Stroke.所以在禁用时显示为灰色,鼠标结束时显示不同的颜色.标准的东西......

<Style x:Key="BasePathButton" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Name="Border"
                    <Path x:Name="Path" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Path" Property="Stroke" Value="Gray"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Path" Property="Stroke" Value="Green"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Path" Property="Stroke" Value="Red"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

但显然我需要为每个按钮实例使用不同的Path.Data.所以我创建了一个用于设置Path.Data的BasedOn样式,就像这样......

<Style x:Key="NLB" TargetType="{x:Type Button}" BasedOn="{StaticResource BasePathButton}">
    <Setter TargetName="Path" Property="Data" Value="{StaticResource LeftPathGeometry}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

...但是失败了,发现无法找到TargetName ="Path"的错误.任何想法如何解决这一问题?或者也许是一种更好的方法来创建一个具有Path的按钮,该Path使用要使用的几何参数化?

wpf styles button pathgeometry

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