我设计了一个包含两个Button
的{{1}}元素。一个是实际文本,另一个是图标(来自Segoe UI符号)。
这是样式的代码:
TextBlock
问题:
我还希望Icon-TextBlock有一个自定义图标,因此是一个自定义<Style x:Key="ButtonSettingsStyle" TargetType="Button">
<Setter Property="Content" Value=""/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<StackPanel Background="Transparent" Orientation="Horizontal" Height="60">
<VisualStateManager.VisualStateGroups>
...
</VisualStateManager.VisualStateGroups>
<TextBlock
x:Name="Icon"
Text=""
Margin="10,0,5,0"
Width="40"
Foreground="{TemplateBinding Foreground}"
FontSize="32"
VerticalAlignment="Center"
RenderTransformOrigin="0.5,0.5"
FontFamily="Segoe UI Symbol"></TextBlock>
<TextBlock
x:Name="Text"
Text="{TemplateBinding Content}"
VerticalAlignment="Center"
Style="{StaticResource TextBlockListBoldItem}"
Foreground="{TemplateBinding Foreground}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
- 属性,并且可能就像这样(我真的不知道这应该如何工作):
Text
我怎样才能做到这一点?
<Button Style="{StaticResource ButtonSettingsStyle}" Content="Settings" IconContent="" />
- Setter已分配给Text-TextBlock ...
感谢您的帮助!
答案 0 :(得分:2)
编辑:我同意@meilke评论,但问题是您需要一个dependencyproperty来设置TemplateBinding,所以请看下面的新类ButtonWithIcon
您应该从Button继承并添加名为IconContent的DependencyProperty:
public class ButtonWithIcon : Button
{
public static readonly DependencyProperty IconContentProperty =
DependencyProperty.Register("IconContent", typeof (string), typeof (ButtonWithIcon), new PropertyMetadata(default(Icon)));
public string IconContent
{
get { return (string) GetValue(IconContentProperty); }
set { SetValue(IconContentProperty, value); }
}
}
并像这样使用它:
<Style x:Key="ButtonSettingsStyle" TargetType="local:ButtonWithIcon">
<Setter Property="Content" Value=""/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ButtonWithIcon">
<StackPanel Background="Transparent" Orientation="Horizontal" Height="60">
<VisualStateManager.VisualStateGroups>
...
</VisualStateManager.VisualStateGroups>
<TextBlock
x:Name="Icon"
Text="{Binding IconContent, RelativeSource={RelativeSource TemplatedParent}}"
Margin="10,0,5,0"
Width="40"
Foreground="{TemplateBinding Foreground}"
FontSize="32"
VerticalAlignment="Center"
RenderTransformOrigin="0.5,0.5"
FontFamily="Segoe UI Symbol"></TextBlock>
<TextBlock
x:Name="Text"
Text="{TemplateBinding Content}"
VerticalAlignment="Center"
Style="{StaticResource TextBlockListBoldItem}"
Foreground="{TemplateBinding Foreground}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<local:ButtonWithIcon Style="{StaticResource ButtonSettingsStyle}" Content="Settings" IconContent="" />
答案 1 :(得分:1)
使用自定义属性Button
的子类IconContent
来实现该目标。
这是新课程:
public class MyButton : Button
{
public static readonly DependencyProperty IconContentProperty =
DependencyProperty.Register("IconContent", typeof (string), typeof (ButtonWithIcon), new PropertyMetadata(default(Icon)));
public string IconContent
{
get { return (string) GetValue(IconContentProperty); }
set { SetValue(IconContentProperty, value); }
}
}
以下是调整后的样式(请注意TemplateBinding
的新IconContent
):
<Style x:Key="ButtonSettingsStyle" TargetType="local:MyButton">
<Setter Property="Content" Value=""/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyButton">
<StackPanel Background="Transparent" Orientation="Horizontal" Height="60">
<VisualStateManager.VisualStateGroups>
...
</VisualStateManager.VisualStateGroups>
<TextBlock
x:Name="Icon"
Text="{TemplateBinding IconContent}"
Margin="10,0,5,0"
Width="40"
Foreground="{TemplateBinding Foreground}"
FontSize="32"
VerticalAlignment="Center"
RenderTransformOrigin="0.5,0.5"
FontFamily="Segoe UI Symbol"></TextBlock>
<TextBlock
x:Name="Text"
Text="{TemplateBinding Content}"
VerticalAlignment="Center"
Style="{StaticResource TextBlockListBoldItem}"
Foreground="{TemplateBinding Foreground}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
以下是用法:
<local:MyButton Style="{StaticResource ButtonSettingsStyle}" Content="Settings" IconContent="" />
答案 2 :(得分:1)
如果您不想将Button子类化为添加自己的属性,则可以使用Tag属性或an attached property。
请注意,如果您使用附加属性,则需要绑定到TemplatedParent
而不是TemplateBinding
:
{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=local:AttachedProperties.IconContent}
我不知道你为什么要限制按钮只接受文字。通常按钮接受任何内容并使用ContentPresenter s。就个人而言,我会将IconContent属性键入object
并定义模板,如下所示:
<ContentPresenter x:Name="Icon" Content="{TemplateBinding IconContent}" />
<ContentPresenter x:Name="Text" />
然后像这样设置:
<Button>
<Button.IconContent>
<TextBlock Style="{StaticResource IconTextBlockStyle}" Text="" />
</Button.IconContent>
</Button>