0

是否可以在 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>
4

2 回答 2

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 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 上设置任何其他内容,例如<Button Content="..." /><Button ...>SomeContent</Button>,因为这会覆盖样式中的“占位符内容”。

于 2013-01-08T19:31:38.583 回答
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 或命令。

于 2013-01-08T14:30:16.893 回答