【问题标题】:WPF Adding a custom property in a controlWPF 在控件中添加自定义属性
【发布时间】:2013-08-09 03:21:53
【问题描述】:

好的,我有一个 WPF 应用程序,其中有一个资源字典。在字典中,我有一个 Button 的新样式,看起来像这样:

    <Style x:Key="MenuButtonStyle" TargetType="Button">
      <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Grid>
                    <Image x:Name="Imager" RenderOptions.BitmapScalingMode="HighQuality" Width="{TemplateBinding Width}"  Height="{TemplateBinding Height}" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="UniformToFill"/>
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="Imager" Property="Width" Value="24" />
                        <Setter TargetName="Imager" Property="Height" Value="24" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Imager" Property="Width" Value="16" />
                        <Setter TargetName="Imager" Property="Height" Value="16" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

并像这样在我的 XAML 中使用这种风格:

<Button x:Name="Home" Style="{DynamicResource MenuButtonStyle}" Width="20" Height="20" Tag="\Images\Home.png"/>
<Button x:Name="Settings" Style="{DynamicResource MenuButtonStyle}" Width="20" Height="20" Tag="\Images\Settings.png"/>

现在我想做的是,以Button 样式创建一个名为OverWidthOverHeight 的新属性,以便像这样使用它:

<Trigger Property="IsMouseOver" Value="true">
   <Setter TargetName="Imager" Property="Width" Value= OVERWIDTH/>
   <Setter TargetName="Imager" Property="Height" Value= OVERHEIGHT/>
</Trigger>

在 XAML 中:

<Button x:Name="Home" Style="{DynamicResource MenuButtonStyle}" Width="20" Height="20" Tag="\Images\Home.png" OVERWIDTH = "24"/>

我什至不知道这是否可能。我不想使用 C# 代码中的Binding SomeIntenger。 提前致谢。

【问题讨论】:

    标签: c# wpf xaml properties custom-controls


    【解决方案1】:

    您可以使用attached properties:

    public class Extensions
    {
        public static readonly DependencyProperty OverWidthProperty =
            DependencyProperty.RegisterAttached("OverWidth", typeof(double), typeof(Extensions), new PropertyMetadata(default(double)));
    
        public static void SetOverWidth(UIElement element, double value)
        {
            element.SetValue(OverWidthProperty, value);
        }
    
        public static double GetOverWidth(UIElement element)
        {
            return (double)element.GetValue(OverWidthProperty);
        }
    }
    

    Xaml:

    xmlns:extensions="clr-namespace:NAMESPACE FOR Extensions class"
    
    <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="MinWidth" Value="{Binding Path=(extensions:Extensions.OverWidth), RelativeSource={RelativeSource Self}}"/>
    </Trigger>
    
    <Button extensions:Extensions.OverWidth="100" Width="10"/>
    

    【讨论】:

    • 不适合我。我使用画笔而不是双。默认元数据值的设置与 xaml 中设置的画笔值无关。知道为什么会这样吗?
    【解决方案2】:

    我使用了标签属性,效果很好。我可以在Tag

    上写任何东西

    例如,按钮上的 (AXML):

    <Button x:Name="Btn80" Click="eventoClick" Tag="80">Casillero 80</Button>}
    

    我可以使用字符串字段添加 listsvalues intdate 等,然后 解释并将字符串转换成任何东西。

    **我使用了这个解决方案,因为需要给按钮添加一个ID字段。

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 2011-04-16
      • 2015-06-03
      • 2018-04-09
      相关资源
      最近更新 更多