【问题标题】:WPF Style override HeightWPF 样式覆盖高度
【发布时间】:2018-09-14 20:12:39
【问题描述】:

我的 xaml 文件中有一个来自第三方库的元素

<core:CheckBox />

它在那个库中有它自己的风格

<Style x:Key="{x:Type controls:CheckBox}" TargetType="{x:Type controls:CheckBox}">
    <Style.Setters>
      <Setter Property="FrameworkElement.Width" Value="170" />
      <Setter Property="FrameworkElement.Height" Value="60" />
      <Setter Property="Control.Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type controls:CheckBox}">
            <controls:Button x:Name="Button" 
              Width="{TemplateBinding FrameworkElement.Width}" 
              Height="{TemplateBinding FrameworkElement.Height}"
              ...
          </ControlTemplate>
        </Setter.Value>
      </Setter>
      ...
</Style>

不幸的是,由于设置了高度和宽度,它不想拉伸。

我无法修改第三方库中的样式。情况有其优点,因为如果他们改变样式,我的元素将与系统的其他部分具有统一的外观。

我想使用样式,但不知何故放弃了高度和宽度设置器。 有什么想法吗?

编辑: CheckBox 在 Grid 中,它不会填充单元格,这就是目标。

【问题讨论】:

    标签: .net wpf dependency-properties wpf-style


    【解决方案1】:

    您可以根据第三方默认样式创建自己的默认样式并在那里重置属性。我找到了一种使用值转换器的方法:

    public class DummyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
            // return DependencyProperty.UnsetValue;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
    

    这个转换器什么都不做,也不为绑定提供价值。

    <local:DummyConverter x:Key="Resetter"/>
    <Style TargetType="{x:Type controls:CheckBox}" 
           BasedOn="{StaticResource {x:Type controls:CheckBox}}">
        <Setter Property="Height" 
                Value="{Binding RelativeSource={RelativeSource Self}, 
                                Converter={StaticResource Resetter}}"/>                    
        <Setter Property="Width" 
                Value="{Binding RelativeSource={RelativeSource Self}, 
                                Converter={StaticResource Resetter}}"/>
    </Style>
    

    宽度和高度不是故意设置的

    【讨论】:

    • 感谢您的帮助。我添加了更多信息。 CheckBox 位于 Grid 中,但仍无法填充单元格。
    • @Dela,wpf 复选框通常是一个小方块。如果它必须填充大的矩形区域 - 那么它需要HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 甚至是一个新模板,我认为
    • 它有一个模板,它实际上是一个按钮,我也将这个添加到问题中
    【解决方案2】:

    答案在 Ash 的评论中

    <Style x:Key="{x:Type controls:CheckBox}" TargetType="{x:Type controls:CheckBox}">
        <Style.Setters>
          <Setter Property="FrameworkElement.Width" Value="170" />
          <Setter Property="FrameworkElement.Height" Value="60" />
          <Setter Property="Control.Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type controls:CheckBox}">
                <controls:Button x:Name="Button" 
                  Width="{TemplateBinding FrameworkElement.Width}" 
                  Height="{TemplateBinding FrameworkElement.Height}"
                  <!-- ADDED ALIGNMENT -->
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  ...
              </ControlTemplate>
            </Setter.Value>
          </Setter>
          ...
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      相关资源
      最近更新 更多