WPF:Style中的动态属性值

时间:2013-01-08 14:20:40

标签: wpf xaml

是否可以在WPF样式中使用 PlaceHolder ,以后可以通过实现控件为其分配值?类似的东西:

<Style x:Key="BigButton" TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="#58290a" />
    <Setter Property="FontFamily" Value="Lucida Console" />
    <Setter Property="FontSize" Value="24" />
    <Setter Property="Content">
        <Setter.Value>
            <StackPanel>
                <Image Source=[SOME PLACEHOLDER]></Image>
                <TextBlock>[ANOTHER PLACEHOLDER]</TextBlock>
            </StackPanel>
        </Setter.Value>
    </Setter>
</Style>

然后一个Button可以像这样为这个占位符分配一个图像路径:

<Button Name="btn" Style="{StaticResource BigButton}">
    <SOME CLEVER WAY OF ASSIGNING A PATH TO Style.Content.StackPanel.Image.Source and Style.Content.StackPanel.TextBlock.Text>
</Button>

2 个答案:

答案 0 :(得分:1)

这听起来像是Attached properties的工作。

使用“占位符属性”创建实用程序类:

public static class BigButtonUtils
{
    public static Uri GetBBImage(DependencyObject obj)
    {
        return (Uri)obj.GetValue(BBImageProperty);
    }
    public static void SetBBImage(DependencyObject obj, Uri value)
    {
        obj.SetValue(BBImageProperty, value);
    }
    // Using a DependencyProperty as the backing store for BBImage.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BBImageProperty =
        DependencyProperty.RegisterAttached("BBImage", typeof(Uri), typeof(BigButtonUtils), new UIPropertyMetadata(null));


    public static string GetBBCaption(DependencyObject obj)
    {
        return (string)obj.GetValue(BBCaptionProperty);
    }
    public static void SetBBCaption(DependencyObject obj, string value)
    {
        obj.SetValue(BBCaptionProperty, value);
    }
    // Using a DependencyProperty as the backing store for BBCaption.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BBCaptionProperty =
        DependencyProperty.RegisterAttached("BBCaption", typeof(string), typeof(BigButtonUtils), new UIPropertyMetadata(null));
}

在Button上应用这些属性..

<Button Style="{StaticResource BigButtonStyle}"
        bb:BigButtonUtils.BBImage="http://programming.enthuses.me/1.png" 
        bb:BigButtonUtils.BBCaption="My caption" />

..并使用Style中的属性值:

<Style x:Key="BigButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="#58290a" />
    <Setter Property="FontFamily" Value="Lucida Console" />
    <Setter Property="FontSize" Value="24" />
    <Setter Property="Content">
        <Setter.Value>
            <StackPanel DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}">
                <Image Source="{Binding Path=(bb:BigButtonUtils.BBImage)}" Stretch="None" />
                <TextBlock Text="{Binding Path=(bb:BigButtonUtils.BBCaption)}" />
            </StackPanel>
        </Setter.Value>
    </Setter>
</Style>

注意:请勿在按钮上设置任何其他内容,例如<Button Content="..." /><Button ...>SomeContent</Button>,因为这会覆盖样式中的“占位符内容”。< / p>

答案 1 :(得分:0)

您可以使用属性作为占位符:

    <Style x:Key="BigButton" TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="#58290a" />
        <Setter Property="FontFamily" Value="Lucida Console" />
        <Setter Property="FontSize" Value="24" />
        <Setter Property="Content">
            <Setter.Value>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Placeholder}"></TextBlock>
                </StackPanel>
            </Setter.Value>
        </Setter>
    </Style>

财产:

    public string _placeholder;
    public string Placeholder
    {
        get
        {
            return _placeholder;
        }
        set
        {
            _placeholder = value;
            OnPropertyChanged("Placeholder");
        }
    }

现在您可以根据需要修改此属性,例如通过OnClick-EventHandler或Command。