gpt4 book ai didi

wpf - 使用样式触发器增加用户控件大小?

转载 作者:行者123 更新时间:2023-12-04 05:38:44 25 4
gpt4 key购买 nike

当“IsSelected”DP 设置为 true 时,我想使自定义 UserControl 以乘数增长。我当前的 XAML 如下所示:

<ctrl:MyBaseControl x:Class="MyDemo.Controls.MyCustomControl"
...>
<ctrl:MyBaseControl.Resources>
<Style TargetType="{x:Type ctrl:MyCustomControl}">
<Setter Property="BorderBrush" Value="White" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Width" Value="340" />
<Setter Property="Height" Value="260" />
</Trigger>
</Style.Triggers>
</Style>
</ctrl:MyBaseControl.Resources>
<Border>
<StackPanel>
...
</StackPanel>
</Border>

在上面的示例中,“MyBaseControl”扩展了 UserControl,并定义了 IsSelected DP。

这段代码目前无法正常工作,这是我的问题之一。另一个是我想将宽度/高度增加一定数量(例如:0.10)而不是将其设置为硬数字。这样我就可以在源定义控件时设置大小。

感谢您的帮助!

附加代码:

MyBaseControl 代码:
public abstract class MyBaseControl: UserControl
{
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register(
"IsSelected",
typeof(Boolean),
typeof(MyBaseControl),
new PropertyMetadata(null));

public MyBaseControl() : base() { }

#region Properties

public Boolean IsSelected
{
get { return (Boolean)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}

#endregion Properties

}

MyCustomControl 代码:
public partial class MyCustomControl: MyBaseControl
{
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register(
"Icon",
typeof(ImageSource),
typeof(MyCustomControl),
new PropertyMetadata(null));

public static readonly DependencyProperty BlurbProperty =
DependencyProperty.Register(
"Blurb",
typeof(String),
typeof(MyCustomControl),
new PropertyMetadata(null));

public MyCustomControl()
{
InitializeComponent();
}

#region Properties

public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}

public String Blurb
{
get { return (String)GetValue(BlurbProperty); }
set { SetValue(BlurbProperty, value); }
}

#endregion Properties
}

内部元素的工作触发器示例:
    <Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ctrl:MyCustomControl}}, Path=IsSelected}" Value="True">
<Setter Property="BorderThickness" Value="5" />
</DataTrigger>
</Style.Triggers>
</Style>

最佳答案

试试这个

<ctrl:MyBaseControl.Resources>
<Style TargetType="{x:Type ctrl:MyCustomControl}">
<Setter Property="BorderBrush" Value="White" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderThickness" Value="2" />
<Setter Property="RenderTransform" >
<Setter.Value>
<ScaleTransform ScaleX="1.1" ScaleY="1.1" />
</Setter.Value>
</Setter>
<Setter Property="RenderTransformOrigin" Value="0.5, 0.5"/>
</Trigger>
</Style.Triggers>
</Style>
</ctrl:MyBaseControl.Resources>

关于wpf - 使用样式触发器增加用户控件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11568214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com