问题与按钮模板在wpf..

本文关键字:wpf 按钮 问题 | 更新日期: 2023-09-27 18:08:45

我是wpf的新手。我想在buttoncontrol中显示一个图像和一个文本块。因此,我创建了一个模板,并将图像绑定到源,将文本块绑定到名称。

<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
        <ControlTemplate.Resources>
            <Storyboard x:Key="Storyboard1">
                <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="border">
                    <EasingThicknessKeyFrame KeyTime="0:0:0.1" Value="1"/>
                    <EasingThicknessKeyFrame KeyTime="0:0:0.2" Value="0"/>
                </ThicknessAnimationUsingKeyFrames>
            </Storyboard>
        </ControlTemplate.Resources>
        <Border x:Name="border" BorderBrush="Black" BorderThickness="0" Margin="0,0,-96,-21">
            <Grid>
                <Image HorizontalAlignment="Left" Margin="8,8,0,8" Width="34" Source="{Binding Source}"/>
                <TextBlock Margin="46,8,8,8" TextWrapping="Wrap" Text="{Binding Name}" FontSize="16" Foreground="Black"/>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Trigger.ExitActions>
                    <StopStoryboard BeginStoryboardName="Storyboard1_BeginStoryboard"/>
                </Trigger.ExitActions>
                <Trigger.EnterActions>
                    <BeginStoryboard x:Name="Storyboard1_BeginStoryboard" Storyboard="{StaticResource Storyboard1}"/>
                </Trigger.EnterActions>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

然后我将这个模板应用到按钮上。我尝试通过窗口加载事件添加内容。

btnAddGropu.Content = new ToolButton() { Source = "Icons/add.png", Name = "Add Group" };
工具按钮类
class ToolButton
{
    public string Source { get; set; }
    public string Name { get; set; }
}

当我兴奋地执行程序时,我根本没有得到按钮…!!那么问题是什么?我如何使用这个模板…?

谢谢提前

问题与按钮模板在wpf..

假设您已经使用控件模板设置了按钮,如下所示…

    <Button Template="{StaticResource ButtonControlTemplate1}"/>

修复您的问题是设置按钮的DataContext而不是其Content

     btnAddGropu.DataContext
       = new ToolButton() { Source = "Icons/add.png", Name = "Add Group" };

虽然我必须坚持你改变你的方式显示文本和图像通过ControlTemplate总是期望一个"特定"的DataContext即ToolButton .

控件模板和数据绑定、datatemplate和User动作效果永远不能混合在一起。

: -)

是否将此模板设置为按钮?您必须设置它,因为您为ControlTemplate指定了一个名称,这使得它显式。如果没有名称,它将是隐式的,因此它将被自动使用。

<Button Template="{StaticResource ButtonControlTemplate1}"/>

在你的例子中还有几件事我想指出。您将按钮Content设置为类ToolButton的实例,这很好。这个类显然不是WPF ui控件,因此您必须告诉WPF如何处理它。这就是DataTemplate的作用,你为你的ToolButton类定义了一个DataTemplate,现在WPF知道如何显示这个了。

不幸的是,你的按钮Template有另一个问题。按钮的类型为ContentControl,因此允许它显示任何内容。您已经使用了Content属性。但是你完全重写了ControlTemplate,记住,一个ControlTemplate定义了一个控件的样子,没有它的模板,它绝对不会显示任何东西。现在看看你的模板,我错过了一个叫做ContentPresenter的专门控件,这告诉模板"当按钮有内容时,把它放在这里",所以ContentPresenter是实际设置内容的占位符。现在记住我说的关于DataTemplate,你会看到这些东西是如何一起工作的。

另一个小的事情是,你在你的模板中使用绑定,但你从来没有设置DataContext,你只是设置了内容,但内容只在ContentPresenter或DataTemplate中有效。关于DataContext有一些"规则"。快速总结:DataContext可以被继承,并且可以在DataTemplate中自动设置。

我希望这对你有所帮助。欢呼。

我认为你需要这样的东西:

<Button>
  <StackPanel>
    <Ellipse Height="40" Width="40" Fill="Blue"/>
    <TextBlock TextAlignment="Center">Button</TextBlock>
  </StackPanel>
</Button>

MSDN

你可以用Grid或DockPanel替换StackPanel,并在此容器中嵌入任何你想要的控件。

我认为为了正确处理标准控件的自定义,您必须使用ContentPresenter,它具有您想要的按钮的Style