【问题标题】:Extracting same properites to style in WPF XAML在 WPF XAML 中提取相同的属性以设置样式
【发布时间】:2015-07-30 07:46:02
【问题描述】:

我一直在为我的 Xaml 文件中的元素样式提取一些属性。 我有很多重复的块,例如:

<controls:RoundableToggleRadioButton Style="{StaticResource RoundableToggleRadioButtonStyle}">
<StackPanel>
    <Image Width="32"
           Margin="2"
           Source="Images/inbox_upload.png" />
    <TextBlock Margin="2"
               Foreground="White"
               Text="Extract"
               TextAlignment="Center" />
</StackPanel>

所以我想为每个按钮提取相同的属性来设置样式并能够更改图像和文本。像这样的:

<Setter Property="ContentTemplate">
<Setter.Value>
    <DataTemplate>
        <StackPanel>
            <Image Width="32"
                   Margin="2" />
            <TextBlock Margin="2"
                       Foreground="White"
                       TextAlignment="Center" />
        </StackPanel>
    </DataTemplate>
</Setter.Value>

<controls:RoundableToggleRadioButton Style="{StaticResource RoundableToggleRadioButtonStyle}">
<StackPanel>
    <Image Source="Images/inbox_upload.png" />
    <TextBlock Text="Extract"/>
</StackPanel>

那么有可能吗?或者有一些解决方法? 感谢您的帮助)))

【问题讨论】:

    标签: c# wpf xaml styles


    【解决方案1】:

    创建继承Button的新类

    public class ImageTextButton : Button
    {
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof (ImageSource), typeof (ImageTextButton), null);
    
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof (string), typeof (ImageTextButton), null);
    
        public ImageTextButton()
        {
            this.DefaultStyleKey = typeof(ImageTextButton);
        }
    
        public ImageSource Icon
        {
            get { return (ImageSource) GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }
    
        public string Text
        {
            get { return (string) GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
    }
    

    xaml

    <Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication3="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type wpfApplication3:ImageTextButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type wpfApplication3:ImageTextButton}">
                        <StackPanel Height="Auto" Orientation="Horizontal">
                            <Image Source="{TemplateBinding Icon}" Stretch="Fill"/>
                            <TextBlock Text="{TemplateBinding Text}"/>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <wpfApplication3:ImageTextButton Text="Submit" Icon="Hydrangeas.jpg"></wpfApplication3:ImageTextButton>
    </Grid>
    

    您也可以使用 Button 的 Content 属性,而不是创建新的 Text 属性。

    【讨论】:

    • 这比我预期的要难))但它解决了我的问题。谢谢你的时间!!!
    【解决方案2】:

    如果您有这么多 xaml 文件并且必须在任何地方应用相同的样式,您可以使用 应用程序资源 来做到这一点。

    这里有一段代码例如(app.xaml

    <Application.Resources>
      <Style TargetType="Button" x:Key="ButtonStyling" >
         <Setter Property="Margin" Value="1,2,1,2"/>
         <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
    </Application.Resources>
    

    然后,对于您的按钮(例如):

    <Button Height="50" Width="250" Style="{StaticResource ButtonStyling}" Content="Button 1" />
    <Button Height="50" Width="250" Style="{StaticResource ButtonStyling}" Content="Button 2" />
    

    希望这将帮助您找到所需的内容。

    【讨论】:

    • 感谢您的帮助!在我看来,这是我能做的最好的事情。但我搜索了更优雅的方式来做这些事情。任何方式都非常感谢你!!!
    • 您能找到更好的解决方案真是太好了。您可以将任何答案标记为解决方案,以便人们很容易发现您的问题与他们的问题相似。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 2010-11-19
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多