提取相同的属性以在 WPF 中设置样式 XAML
Extracting same properites to style in WPF XAML
我无法在我的 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>
所以有可能吗?或者有一些解决方法?
感谢您的帮助)))
如果您有这么多 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" />
希望这能帮助您找到您要找的东西。
创建继承 Button
的新 class
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>
除了创建新文本 属性,您还可以使用按钮的内容 属性。
我无法在我的 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>
所以有可能吗?或者有一些解决方法? 感谢您的帮助)))
如果您有这么多 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" />
希望这能帮助您找到您要找的东西。
创建继承 Button
的新 classpublic 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>
除了创建新文本 属性,您还可以使用按钮的内容 属性。