【WPF】记录一些易忘的用法
1.设计时属性
在属性前加d:可以设置设计时的属性,特别是使用UserControl时,子控件绑定了属性后就无法从设计器中观察,可以在控件顶部分使用以下语句,在编译后可看到效果。
d:DataContext="{d:DesignInstance Type=local:控件名, IsDesignTimeCreatable=True}"
但是实际效果也要根据DataContent的指向和数据的绑定方式呈现,比如以下这种方式就没有使设计时属性生效:
{Binding 属性名, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:控件名}}}
改成{Binding 属性名}
就生效了,当然在后台我将DataContext = this;
2.寻找模板中的name
TextBox textBox = 要使用模板的元素name.Template.FindName("要获得的元素name", this.要使用模板的元素name) as TextBox;
3.寻找Storyboard
Storyboard Ani = (Storyboard)Resources["StoryboardName"];
4.对一个Storyboard,在Loaded时加载,想通过Trigger控件暂停和继续:
<Storyboard x:Key="Storyboard1" RepeatBehavior="Forever" x:Name="_Ani1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="image">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="360"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard x:Name="_Storyboard1" Storyboard="{StaticResource Storyboard1}"/>
</EventTrigger>
<Trigger Property="IsLeftRun" Value="true">
<Trigger.EnterActions>
<ResumeStoryboard BeginStoryboardName="_Storyboard1"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsLeftRun" Value="false">
<Trigger.EnterActions>
<PauseStoryboard BeginStoryboardName="_Storyboard1"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
5.选中模板内元素
控件使用了模板,在设计器中单击无法选中模板内的元素,可在设计器中右击,选择编辑模板->编辑当前项
6.制作滑动解锁控件
这类控件可以用Slider或者Scrollbar实现,比如Slider,最小值和最大值设为0和1,在鼠标抬起事件中做一些体验上的处理
7.popup的StaysOpen无法自动关闭
某些情况下,popup控件即使设置了StaysOpen=false
,在点击其他区域时也不会自动关闭,可以将StaysOpen
默认设为true
,在Opened事件中也StaysOpen设为true,在窗口添加一个点击事件,事件中将StaysOpen
设为false
,不过如果点击到了其他按钮,是无效的,只能在那个按钮上再单独处理
private void _popup_Opened(object sender, EventArgs e)
{
_popup.StaysOpen = true;
}
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_popup.StaysOpen = false;
}
8.控制Storyboard
可以在后台代码中使用StoryboardKeyName.Begin(TargetElement, true)
,如果要控制它播放和停止,尽量用Resume(TargetElement)
和Pause(TargetElement)
,否则需要控制速率时会有失效的情况。
9.曲线图填充
上图中曲线下面的填充色,实现方法不唯一,这里使用的方法是Path.Data绑定Polyline的Points
<Canvas >
<Path >
<Path.Data>
<PathGeometry>
<PathFigure x:Name="_Start" >
<PolyLineSegment Points="{Binding Points, ElementName=polyline}"/>
<LineSegment x:Name="_End" />
</PathFigure>
</PathGeometry>
</Path.Data>
<Path.Fill>
<LinearGradientBrush StartPoint ="0,0" EndPoint ="0,1">
<GradientStop Color ="{yyy}" Offset ="0.25" ></GradientStop>
<GradientStop Color ="#00000000" Offset ="1"></GradientStop>
</LinearGradientBrush>
</Path.Fill>
</Path>
<Polyline x:Name="polyline" Stroke="{Binding xxx}/>
</Canvas>
_Start.StartPoint = new Point(x1,y1);
_End.Point = new Point(NormalizeX(x2),y2);
10.Behavior中获得鼠标位置并将依赖属性对外绑定
protected override void OnAttached()
{
AssociatedObject.MouseMove += AssociatedObject_MouseMove;
}
private void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
var p = e.GetPosition(element);
X = p.X; //X和Y是依赖属性,这里没贴代码
Y = p.Y;
BindingOperations.GetBindingExpression(this, XProperty).UpdateSource();
BindingOperations.GetBindingExpression(this, YProperty).UpdateSource();
}
<i:Interaction.Behaviors>
<be:MouseActionBehavior X="{Binding X,Mode=OneWayToSource}" Y="{Binding Y,Mode=OneWayToSource}" />
</i:Interaction.Behaviors>
11.指定触发Command的事件
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseUp">
<i:InvokeCommandAction Command="{Binding xxx}"
CommandParameter="{Binding yyy}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?