作为按钮内容的传染媒介象在样式里面不显示在设计时间

时间:2014-04-18 05:19:29

标签: wpf xaml

我想使用矢量图标作为Button样式中的内容,以便在多个按钮(超过20个)中使用。所以,我确实喜欢这个:

我的图标:

<Rectangle x:Key="DefaultsIcon" Height="20" Width="20" x:Shared="False" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="{DynamicResource BlackBrush}">
    <Rectangle.OpacityMask>
        <VisualBrush Stretch="Fill">
            <VisualBrush.Visual>
                <Canvas Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
                    <Path Width="29.26" Height="32.5111" Canvas.Left="24.9269" Canvas.Top="21.3222" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 24.9269,53.8333L 24.9269,21.3222L 36.6106,21.3222C 48.3282,21.3222 54.1869,26.6053 54.1869,37.1714C 54.1869,42.2319 52.588,46.274 49.3901,49.2977C 46.1922,52.3215 41.9324,53.8333 36.6106,53.8333L 24.9269,53.8333 Z M 32.3581,27.36L 32.3581,47.7956L 36.0156,47.7956C 39.2231,47.7956 41.7377,46.8509 43.5591,44.9617C 45.3806,43.0725 46.2914,40.5023 46.2914,37.2512C 46.2914,34.1791 45.3879,31.7625 43.5809,30.0015C 41.7739,28.2405 39.2376,27.36 35.972,27.36L 32.3581,27.36 Z "/>
                </Canvas>
            </VisualBrush.Visual>
        </VisualBrush>
    </Rectangle.OpacityMask>
</Rectangle>

我的风格:

<Style TargetType="Button" BasedOn="{StaticResource CameraButtonBaseStyle}" x:Key="CameraDefaultsButtonStyle">
     <Setter Property="Margin" Value="{Binding Source={StaticResource CameraUISettings}, Path=OptionLBMargin, Mode=OneWay, Converter={uiConverters:DoubleToLeftMarginConverter}}"></Setter>
     <Setter Property="ToolTip" Value="{Binding Source={StaticResource CameraLocalization}, Path=ToolTips.Default, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></Setter>
     <Setter Property="Content" Value="{DynamicResource DefaultsIcon}"></Setter>
</Style>

并应用样式:

<Button Style="{StaticResource CameraDefaultsButtonStyle}"  
                                 Click="LoadDefaultX_OnClick">
</Button>

但问题在于,在设计时只有1个图标显示所有图标并且在运行时所有图标都显示良好。我希望在设计时间图标也可见。 如何达到这个目标?

3 个答案:

答案 0 :(得分:1)

问题是DefaultsIcon样式包含视觉效果。该应用程序的每个生命周期仅创建一次样式,并根据需要重复使用。但视觉效果只能在视觉树中使用一次。快速解决方法是将x:Shared="Falsed"添加到DefaultsIcon。从架构角度来看,这不是最佳解决方案,因为现在每次使用DefaultsIcon都会创建一个新实例。

答案 1 :(得分:0)

因为我想分享一些代码,所以我添加了一个新答案。我的问题是你在其他地方需要DefaultsIcon风格吗?如果不是,您可以执行以下操作以获得相同的结果:

    <Style TargetType="Button" x:Key="CameraDefaultsButtonStyle" x:Shared="False" >
        <Setter Property="Margin" Value="10"></Setter>
        <Setter Property="ToolTip" Value="hello"></Setter>
        <Setter Property="Content">
            <Setter.Value>
                <Path Width="29.26" Height="32.5111" Canvas.Left="24.9269" Canvas.Top="21.3222" Stretch="Fill" Fill="Black" Data="F1 M 24.9269,53.8333L 24.9269,21.3222L 36.6106,21.3222C 48.3282,21.3222 54.1869,26.6053 54.1869,37.1714C 54.1869,42.2319 52.588,46.274 49.3901,49.2977C 46.1922,52.3215 41.9324,53.8333 36.6106,53.8333L 24.9269,53.8333 Z M 32.3581,27.36L 32.3581,47.7956L 36.0156,47.7956C 39.2231,47.7956 41.7377,46.8509 43.5591,44.9617C 45.3806,43.0725 46.2914,40.5023 46.2914,37.2512C 46.2914,34.1791 45.3879,31.7625 43.5809,30.0015C 41.7739,28.2405 39.2376,27.36 35.972,27.36L 32.3581,27.36 Z "/>
            </Setter.Value>
        </Setter>
    </Style>

设置x:Shared="False"的需要对我来说是一个标志,可以考虑使用小型用户控件而不是样式。

答案 2 :(得分:0)

不,我的意思是将迷你用户控件替换为您的CameraDefaultsButtonStyle样式,而不仅仅是该样式中的内容。控件可能如下所示:

<Button x:Class="TabControl1.CameraDefaultsButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             ToolTip="Hello">
    <Grid>
        <Path Width="29.26" Height="32.5111" Stretch="Fill" Fill="Black" Data="F1 M 24.9269,53.8333L 24.9269,21.3222L 36.6106,21.3222C 48.3282,21.3222 54.1869,26.6053 54.1869,37.1714C 54.1869,42.2319 52.588,46.274 49.3901,49.2977C 46.1922,52.3215 41.9324,53.8333 36.6106,53.8333L 24.9269,53.8333 Z M 32.3581,27.36L 32.3581,47.7956L 36.0156,47.7956C 39.2231,47.7956 41.7377,46.8509 43.5591,44.9617C 45.3806,43.0725 46.2914,40.5023 46.2914,37.2512C 46.2914,34.1791 45.3879,31.7625 43.5809,30.0015C 41.7739,28.2405 39.2376,27.36 35.972,27.36L 32.3581,27.36 Z "/>
    </Grid>
</Button>

using System.Windows.Controls;

namespace TabControl1
{
    /// <summary>
    /// Interaction logic for CameraDefaultsButton.xaml
    /// </summary>
    public partial class CameraDefaultsButton : Button
    {
        public CameraDefaultsButton ()
        {
            InitializeComponent ();
        }
    }
}

您可以像标准按钮一样使用它:

<Window x:Class="TabControl1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:TabControl1"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <local:CameraDefaultsButton Margin="301,164,0,0" 
                                HorizontalAlignment="Left" 
                                Width="139" 
                                Height="55"
                                VerticalAlignment="Top"/>
    <local:CameraDefaultsButton Margin="301,236,0,0" 
                                HorizontalAlignment="Left"
                                Width="139" 
                                Height="55"
                                VerticalAlignment="Top"/>
  </Grid>
</Window>

但不要忘记添加您使用它的命名空间:

xmlns:local="clr-namespace:TabControl1"