gpt4 book ai didi

.net - WPF:从 UserControl 转换为 CustomControl

转载 作者:行者123 更新时间:2023-12-02 15:41:20 25 4
gpt4 key购买 nike

我有一个 WPF UserControl - SegmentConrol , 表示一行带有一些文本和一个显示方向 (>) 的数组。

因为我必须自定义此控件样式,所以我决定切换到 CustomControl,因为我读到它在某种程度上更好...

现在,我在从 UC 切换到 CC 时遇到了一些“麻烦”。

特别是,不知道<UserControl.Resources>放在哪里部分。

如果有任何专家可以给我建议,欢迎他们。

这是我的用户控件:

<UserControl x:Class="MyNamespace.ctlWpfPlanDeLigne.SegmentControl"
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"
xmlns:local="clr-namespace:MyNamespace.ctlWpfPlanDeLigne"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="ParentSegmentControl">
<UserControl.Resources>
<local:VisibilityConverter x:Key="VisibilityConverter"/>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
<local:SegmentToStringConverter x:Key="SegmentToStringConverter"/>
</UserControl.Resources>
<Canvas Background="Transparent">
<Line x:Name="line"
X1="{Binding ElementName=ParentSegmentControl, Path=X1}"
Y1="{Binding ElementName=ParentSegmentControl, Path=Y1}"
X2="{Binding ElementName=ParentSegmentControl, Path=X2}"
Y2="{Binding ElementName=ParentSegmentControl, Path=Y2}" IsHitTestVisible="True"/>

<Label x:Name="label"
Foreground="{Binding ElementName=ParentSegmentControl, Path=LabelForeground}"
Background="{Binding ElementName=ParentSegmentControl, Path=LabelBackground}"
Visibility="{Binding ElementName=ParentSegmentControl, Path=IsLabelUsed, Converter={StaticResource BoolToVisibilityConverter}}"
>
<Label.Effect>
<DropShadowEffect BlurRadius="2" Color="White" Opacity="1" RenderingBias="Performance" ShadowDepth="0" />
</Label.Effect>
</Label>
<Polygon Name="arrow" Visibility="{Binding ElementName=ParentSegmentControl, Path=IsArrowUsed, Converter={StaticResource BoolToVisibilityConverter}}"/>
</Canvas>
</UserControl>

下面是Themes/Generic.xaml新 CustomControl 的文件我重构了旧的 UserControl:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace.ctlWpfPlanDeLigne">

<Style TargetType="{x:Type local:SegmentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SegmentControl}">
<Canvas Background="Transparent">
<Line x:Name="line"...

<Label x:Name="label"...
<Polygon x:Name="arrow" ...
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

编辑:

用代码做什么

    public SegmentControl()
{
this.line.StrokeDashCap = PenLineCap.Round;
this.arrow.StrokeLineJoin = PenLineJoin.Round;
this.Background = Brushes.Transparent;
}

this.linethis.arrow没有定义?谢谢。

编辑 2

通用.XAML:

<Style TargetType="{x:Type local:SegmentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SegmentControl}">
<Canvas Background="Transparent">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
<Line x:Name="PART_line"
X1="{Binding ElementName=ParentSegmentControl, Path=X1}"
Y1="{Binding ElementName=ParentSegmentControl, Path=Y1}"
X2="{Binding ElementName=ParentSegmentControl, Path=X2}"
Y2="{Binding ElementName=ParentSegmentControl, Path=Y2}"
IsHitTestVisible="True"/>

<Label x:Name="PART_label"
Foreground="{Binding ElementName=ParentSegmentControl, Path=LabelForeground}"
Background="{Binding ElementName=ParentSegmentControl, Path=LabelBackground}"
Visibility="{Binding ElementName=ParentSegmentControl, Path=IsLabelUsed, Converter={StaticResource BoolToVisibilityConverter}}">
<Label.Effect>
<DropShadowEffect BlurRadius="2" Color="White" Opacity="1" RenderingBias="Performance" ShadowDepth="0" />
</Label.Effect>
</Label>
<Polygon x:Name="PART_arrow"
Visibility="{Binding ElementName=ParentSegmentControl, Path=IsArrowUsed, Converter={StaticResource BoolToVisibilityConverter}}"/>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

.cs:

    public override void OnApplyTemplate()
{
base.OnApplyTemplate();

this.line = this.FindName("PART_line") as Line; // null
this.arrow = this.FindName("PART_arrow") as Polygon; // null
this.label = this.FindName("PART_label") as Label; // null

this.line.StrokeDashCap = PenLineCap.Round;
this.arrow.StrokeLineJoin = PenLineJoin.Round;
}

最佳答案

我不明白为什么不能直接将它们放入资源字典中?然后它们可以被其他样式/模板重复使用。

 <ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace.ctlWpfPlanDeLigne">

<local:VisibilityConverter x:Key="VisibilityConverter"/>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
<local:SegmentToStringConverter x:Key="SegmentToStringConverter"/>

<Style....

编辑:

针对您的第二个问题,您需要在代码中执行此操作:

Line line = this.FindName("line") as Line;
Polygon arrow = this.FindName("arrow") as Polygon;

虽然通常的做法是用“PART_”命名您的控件,所以它显示

Line line = this.FindName("PART_line") as Line;
Polygon arrow = this.FindName("PART_arrow") as Polygon;

此名称对应于 xaml 中给出的 x:Name..

编辑 3:

你需要像这样确保你的窗口知道你的资源字典:

 <Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Namespace.Controls;component/Themes/CustomControls.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</WindowResources>

关于.net - WPF:从 UserControl 转换为 CustomControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5153708/

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