【问题标题】:How to determine if a custom control is in a Toolbar?如何确定自定义控件是否在工具栏中?
【发布时间】:2020-04-17 04:05:33
【问题描述】:

我创建了一个 UserControl 来创建一个ImageButton:

<Button x:Class="myimagebutton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:imagebutton">

    <Grid x:Name="grdButton">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Image Grid.Column="0"
               x:Name="btnImage"
               HorizontalAlignment="Left"
               VerticalAlignment="Center">
        </Image>
        <TextBlock Grid.Column="1"
                   TextWrapping="Wrap"
                   Text="{Binding Text}"
                   VerticalAlignment="Center"
                   Margin="2 0 2 0" />
    </Grid>
</Button>

现在我想将 default Toolbar Button Style 应用到我的按钮(如果此按钮位于工具栏中)。我已经阅读了这篇文章link text并把这个

If Me.Style Is Nothing AndAlso TypeOf Me.Parent Is ToolBar Then
   Me.Style = DirectCast(FindResource(ToolBar.ButtonStyleKey), Style)
End If

在我的代码后面。

之后,作为测试,我将我的 Button 放在工具栏和工具栏中的另一个以测试它。但是 Button 总是获得默认样式,而不是我要设置的样式。

调试后我发现Me.Parent 总是Nothing。所以现在我的问题是:我如何获得我的按钮是否在工具栏中的信息?

【问题讨论】:

    标签: wpf vb.net user-controls styles


    【解决方案1】:

    我很难准确理解您所描述的内容,但在阅读了几次之后,我想我明白了。

    到目前为止我是对的吗?

    如果是这样,您想知道为什么您的按钮有图像

    关于你的描述的一些提示让我很反感,这可能是你到目前为止还没有看到其他人为你的问题发布答案的原因。

    我用按钮替换了 UserControl 项

    基本上你所做的是创建了可能继承自 Button 的新控件。您可能从 UserControl 开始,但为了替换 XAML 中的根项,您还必须确保您的类型 myimagebutton 也继承自 Button。这就是 XAML 的工作原理,学习如何以这种方式解释它将有助于人们理解您在做什么。

    通常从 Button 继承并不是开发人员在 WPF 中覆盖按钮的视觉样式的方式,主要是因为 WPF 不支持有时称为visual inheritance 的概念,而且还有其他合适的方法可以使用以不同的方式解决问题。相反,继承主要保留在需要对现有控件类进行行为修改或添加时。这就是说,有一些方法可以通过使用类似于 ASP.NET 中的内容页面和母版页的内容控件来模拟视觉继承,但我认为这有点超出了您的示例范围。此外,如果您要追求继承模型,您将需要确保在您的代码中,您在静态构造函数中设置正确的默认样式,以便为您的按钮发布您的代码也将有所帮助。

    我相信您的示例不起作用的原因是因为 ToolBar 专门查看控件的类型而不考虑继承,以便应用它的自定义工具栏样式。在您的情况下,您的控件的类型是 myimagebutton 而不是 Button,因此 ToolBar 不设置样式,它通常使用两种可能的调用类型根据控件的类型直接设置 Style 属性。

    element.SetResourceReference(FrameworkElement.StyleProperty, styleKey);
    element.DefaultStyleKey = styleKey;
    

    顺便说一句,在您的情况下,我相信只有第二行是由 ToolBar 控件执行的,并且此时 styleKey 被定义为 null。

    现在,与其首先从 Button 继承,不如为按钮创建一个新的 ControlTemplate 或 DataTemplate 并通过使用样式分别分配给 Template 或 ContentTemplate 属性。这样,您仍然总是在处理一个按钮,而样式会改变视觉属性。

    <Window x:Class="HeaderedContentControlTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow"
            Height="252"
            Width="372">
        <Window.Resources>
            <Style TargetType="{x:Type Label}">
                <Setter Property="Background"
                        Value="Orange" />
            </Style>
    
            <DataTemplate x:Key="ImageButtonDataTemplate">
                <Grid x:Name="grdButton">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
    
                    <Image Grid.Column="0"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center">
                    </Image>
                    <TextBlock Grid.Column="1"
                               TextWrapping="Wrap"
                               Text="{Binding}"
                               VerticalAlignment="Center"
                               Margin="2 0 2 0"
                               Background="Pink" />
                </Grid>
            </DataTemplate>
    
            <Style x:Key="ImageButtonStyle"
                   TargetType="{x:Type Button}">
                <Setter Property="ContentTemplate"
                        Value="{StaticResource ImageButtonDataTemplate}" />
            </Style>
    
        </Window.Resources>
        <Grid Margin="11">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
    
            <ToolBar>
                <Button Style="{StaticResource ImageButtonStyle}"
                        Content="Some Text" />
            </ToolBar>
    
            <Button Grid.Row="1"
                    Style="{StaticResource ImageButtonStyle}"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Content="Some Text" />
        </Grid>
    </Window>
    

    使用 ContentTemplate 允许您重新定义按钮的内部内容,而不会丢失所有特殊按钮状态转换和您通常希望保留的其他细节。

    请参阅 MSDN 论坛上的 this related post,其中还解释了将包含按钮的 StackPanel 添加到工具栏时的类似行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多