按钮自定义内容不会在运行时呈现

时间:2016-04-11 15:21:29

标签: c# wpf winforms wpf-style

我在Windows窗体表单中托管了UserControl。在这个UserControl我有ToolBar我有各种按钮:

<ToolBar>
   <Button Content="{StaticResource AllGreenIcon}"/>
   <Button Content="{StaticResource AllRedIcon}"/>
   <Button Content="{StaticResource RedRectangle}"/>
   <Button Content="{StaticResource GreenRectangle}"/>
</ToolBar>

在desinger中看起来像这样:

Toolbar with buttons in Designer mode

问题在于图标由4个矩形组成的按钮。在运行时,这两个按钮的内容不会呈现。

在运行时看起来像这样:

Toolbar at runtime

AllGreenIcon的代码:

<UserControl.Resources>

<Grid x:Key="AllGreenIcon" Height="16" Width="16" Effect="{StaticResource IconDropShadowEffect}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ContentControl Content="{StaticResource GreenRectangle}" Margin="0,0,1,1" Grid.Row="0" Grid.Column="0"/>
    <ContentControl Content="{StaticResource GreenRectangle}" Margin="1,0,0,1" Grid.Row="0" Grid.Column="1"/>
    <ContentControl Content="{StaticResource GreenRectangle}" Margin="0,1,1,0" Grid.Row="1" Grid.Column="0"/>
    <ContentControl Content="{StaticResource GreenRectangle}" Margin="1,1,0,0" Grid.Row="1" Grid.Column="1"/>
</Grid>


</UserControl.Resources>

有没有人有一些想法我怎么能解决这个问题? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

这个常见问题是由于每个UIElement的WPF(逻辑)要求具有单个父级。在您的情况下,您要在资源中添加一个元素 - GreenRectangle,然后您在Content资源中使用此元素作为多个ContentControl的{​​{1}}。每次将元素连接到可视树时,它将更改其父引用,这保证元素在可视树中仅存在一次。 例如,所有绿色按钮都将使用AllGreenIcon元素的相同实例。由于每次GreenRectangle连接到可视树时,其父级都会被更改,因此使用GreenRectangle资源的最后一项将实际显示该元素。

总之,避免在资源中声明和使用GreenRectange。您应该使用UIElementsStyle s。

注意:在您的解决方案中,资源中声明的Controltemplate网格具有相同的问题 - 不能同时在UI中的两个不同位置使用。请改用AllGreenIcon

<强>实施例

ContentTemplate

答案 1 :(得分:0)

我设法通过反复试验找到了溶剂。基本上我所做的就是用ContentControl替换了Rectangle s并为Rectangle创建了一个样式。

<LinearGradientBrush x:Key="GreenLinearGradientBrush" EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
    <GradientStop Color="#FF00BD22" Offset="1"/>
    <GradientStop Color="#FF218934"/>
</LinearGradientBrush>

<Style x:Key="GreenRectangleStyle" TargetType="{x:Type Rectangle}">
   <Setter Property="Width" Value="16"/>
   <Setter Property="Height" Value="16"/>
   <Setter Property="Fill" Value="{StaticResource GreenLinearGradientBrush}"/>
   <Setter Property="Effect" Value="{StaticResource IconDropShadowEffect}"/>
</Style>

<Grid x:Key="AllGreenIcon" Height="16" Width="16" Effect="{StaticResource IconDropShadowEffect}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Rectangle Margin="0,0,1,1" Grid.Row="0" Grid.Column="0" Style="{StaticResource GreenRectangleStyle}"/>
    <Rectangle Margin="1,0,0,1" Grid.Row="0" Grid.Column="1" Style="{StaticResource GreenRectangleStyle}"/>
    <Rectangle Margin="0,1,1,0" Grid.Row="1" Grid.Column="0" Style="{StaticResource GreenRectangleStyle}"/>
    <Rectangle Margin="1,1,0,0" Grid.Row="1" Grid.Column="1" Style="{StaticResource GreenRectangleStyle}"/>
</Grid>

有没有人有更好的解决方案?

修改

@Novitchi S有一个更好的解决方案,基于他的答案,这里是最终版本:

<Style x:Key="GreenRectangleStyle" TargetType="{x:Type Rectangle}">
    <Setter Property="Width" Value="16"/>
    <Setter Property="Height" Value="16"/>
    <Setter Property="Fill" Value="{StaticResource GreenLinearGradientBrush}"/>
    <Setter Property="Effect" Value="{StaticResource IconDropShadowEffect}"/>
</Style>

<DataTemplate x:Key="GreenRectangle">
    <Rectangle  Style="{StaticResource GreenRectangleStyle}"/>
</DataTemplate>

<DataTemplate x:Key="AllGreenIcon">
    <Grid  Height="16" Width="16" Effect="{StaticResource IconDropShadowEffect}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <ContentControl ContentTemplate="{StaticResource GreenRectangle}" Margin="0,0,1,1" Grid.Row="0" Grid.Column="0"/>
        <ContentControl ContentTemplate="{StaticResource GreenRectangle}" Margin="1,0,0,1" Grid.Row="0" Grid.Column="1"/>
        <ContentControl ContentTemplate="{StaticResource GreenRectangle}" Margin="0,1,1,0" Grid.Row="1" Grid.Column="0"/>
        <ContentControl ContentTemplate="{StaticResource GreenRectangle}" Margin="1,1,0,0" Grid.Row="1" Grid.Column="1"/>
    </Grid>
</DataTemplate>

。 。

<ToolBar>
    <Button ContentTemplate="{StaticResource AllGreenIcon}"/>
    <Button ContentTemplate="{StaticResource AllRedIcon}"/>
    <Button ContentTemplate="{StaticResource RedRectangle}"/>
    <Button ContentTemplate="{StaticResource GreenRectangle}"/>
</ToolBar>