将两个UserControl合并为一个有两个状态的正确方法是什么?

问题描述 投票:1回答:2

我在互联网和StackOverflow上搜索了一下。为什么我需要这个:我有两个UserControls叫做PlusButtonMinusButton,它们都有常见的XAML和常见的代码隐藏。我想使用一个通用的基类,理想情况下也会有XAML和代码隐藏。

我已经看到了here发布的答案,但我对XAML没有足够的经验来应用该解决方案。我很高兴有一个官方文档页面的链接,了解我需要做什么。我习惯于代码隐藏,因为我也使用WinForms所以我不介意我是否必须执行代码隐藏。

我比XAML更容易理解代码隐藏,所以我有很多代码隐藏,可能在XAML中做得更好。

如果我使用here发布的解决方案,我会遇到编译错误:

  • cs_wpf_test_1.ArrowButton不能是XAML文件的根,因为它是使用XAML定义的。第1行位置20。
  • 警告CS0108 PlusButton.InitializeComponent()隐藏继承成员ArrowButton.InitializeComponent()。如果要隐藏,请使用new关键字。

我得到上面列出的两个编译器错误,我不知道这些错误最正确的解决方法是什么。我希望它易于维护。

在我遇到编译器错误之前,下面的代码是我的代码的一个版本。

PlusButton.xaml

<UserControl x:Class="cs_wpf_test_1.PlusButton"
             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" 
             xmlns:local="clr-namespace:cs_wpf_test_1"
             mc:Ignorable="d" 
             d:DesignHeight="120" d:DesignWidth="175"
             Loaded="UserControl_Loaded">
    <Button Name="MyButton" Focusable="False" Padding="0,0,0,0">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}"
                         BorderBrush="{TemplateBinding BorderBrush}"
                         BorderThickness="{TemplateBinding BorderThickness}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates" CurrentStateChanged="CommonStates_CurrentStateChanged">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="Pressed" />
                            <VisualState x:Name="Disabled" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                Name="MyCanvas">
                            <Polyline Stroke="Blue" Name="MyPolyline">
                                <Polyline.Points>
                                    <PointCollection>
                                        <Point X="5" Y="95" />
                                        <Point X="95" Y="95" />
                                        <Point X="50" Y="5" />
                                        <Point X="5" Y="95" />
                                    </PointCollection>
                                </Polyline.Points>
                            </Polyline>
                        </Canvas>
                        <Canvas HorizontalAlignment="Center" VerticalAlignment="Center"
                                Width="100" Height="100" Name="MySecondCanvas">
                            <Line Stroke="Black" X1="50" Y1="10"
                                  X2="50" Y2="90"
                  Name="MySignLine1" StrokeThickness="4"/>
                            <Line Stroke="Black" X1="10" Y1="50"
                                  X2="90" Y2="50"
                  Name="MySignLine2" StrokeThickness="4"/>
                        </Canvas>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Button.Template>
    </Button>
</UserControl>

PlusButton.xaml.cs

public partial class PlusButton : UserControl
{
    internal Polyline MyTemplatePolyline;
    internal Line MyTemplateSignLine1, MyTemplateSignLine2;
    internal Canvas MyTemplateCanvas,
        MyTemplateSecondCanvas;

    public PlusButton()
    {
        InitializeComponent();

        MyButton.Margin = new Thickness(
            -MyButton.BorderThickness.Left,
            -MyButton.BorderThickness.Top,
            -MyButton.BorderThickness.Right,
            -MyButton.BorderThickness.Bottom);
    }

    internal void SetPseudofocused(bool p)
    {
        if (p)
        {
            BorderBrush = Brushes.Blue;
        }
        else
        {
            BorderBrush = Brushes.Transparent;
        }
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        MyButton.ApplyTemplate();

        MyTemplatePolyline = (Polyline)MyButton.Template.FindName("MyPolyline", MyButton);
        MyTemplateSignLine1 = (Line)MyButton.Template.FindName("MySignLine1", MyButton);
        MyTemplateSignLine2 = (Line)MyButton.Template.FindName("MySignLine2", MyButton);
        MyTemplateCanvas = (Canvas)MyButton.Template.FindName("MyCanvas", MyButton);
        MyTemplateSecondCanvas = (Canvas)MyButton.Template.FindName("MySecondCanvas", MyButton);

        UpdateMyLayout();
    }

    private void CommonStates_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
    {
        var btn = e.Control as Button;

        if (e.NewState.Name == "MouseOver")
        {
            btn.Background = Brushes.White;
        }
        else if (e.NewState.Name == "Pressed")
        {
            btn.Background = Brushes.LightBlue;
        }
        else if (e.NewState.Name == "Disabled")
        {
            btn.Background = Brushes.Gray;
        }
        else
        {
            btn.Background = (Brush)MyButton.FindResource(SystemColors.ControlBrushKey);
        }
    }

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);

        if (e.Property == WidthProperty || e.Property == HeightProperty || e.Property == BorderThicknessProperty)
        {
            UpdateMyLayout();
        }
    }

    internal void UpdateMyLayout()
    {
        if (MyTemplateCanvas == null)
        {
            return;
        }

        MyTemplateCanvas.Height = Height;
        MyTemplateCanvas.Width = Width;

        double h = ActualHeight - BorderThickness.Top -
            BorderThickness.Bottom;
        double w = ActualWidth - BorderThickness.Left -
            BorderThickness.Right;

        MyTemplatePolyline.Points.Clear();
        MyTemplatePolyline.Points.Add(new Point(5, h - 5));
        MyTemplatePolyline.Points.Add(new Point(w - 5, h - 5));
        MyTemplatePolyline.Points.Add(new Point(w / 2, 5));
        MyTemplatePolyline.Points.Add(new Point(5, h - 5));

        h = MyTemplateSecondCanvas.ActualHeight;
        w = MyTemplateSecondCanvas.ActualWidth;

        double ltrbPadding = h / 3;

        double l1 = h - 2 * ltrbPadding;
        double l2 = w - 2 * ltrbPadding;

        double l = Math.Min(l1, l2);

        // draw a cross with two lines:
        MyTemplateSignLine1.X1 = l / 2d + ltrbPadding;
        MyTemplateSignLine1.X2 = l / 2d + ltrbPadding;
        MyTemplateSignLine1.Y1 = ltrbPadding;
        MyTemplateSignLine1.Y2 = l + ltrbPadding;

        MyTemplateSignLine2.X1 = ltrbPadding;
        MyTemplateSignLine2.X2 = l + ltrbPadding;
        MyTemplateSignLine2.Y1 = ltrbPadding + l / 2d;
        MyTemplateSignLine2.Y2 = ltrbPadding + l / 2d;

        // update focus border size:
        double v = ActualHeight / 25d;
        BorderThickness = new Thickness(v, v, v, v);
    }
}

MinusButton.xaml

与第一个XAML文件相同,但MySecondCanvas的XAML只包含一个Line

MinusButton.xaml.cs

与第一个.cs文件相同,但是:

  1. 它不会创建或使用MyTemplateSignLine1MyTemplateSignLine2,它只使用MyTemplateSignLine
  2. MyTemplatePolyline包含其他看起来像向下箭头的点(第一个箭头,倒置)。
  3. MyTemplateSignLine就像来自其他XAML文件和代码隐藏的MyTemplateSignLine2
c# wpf xaml inheritance user-controls
2个回答
1
投票

WPF中的继承是一件棘手的事情,如果您只想修改代码,您可以继承CS文件中的控件,并根据需要覆盖方法,但如果您想修改xaml,它可能会变得复杂。

但是我相信WPF的方式更倾向于造型/模板。

添加资源字典相当容易(here is how)。如果您想更改按钮背景,您可以添加一个在触发器上更改它的样式。您可能还希望删除按钮的默认行为,您可以通过覆盖模板来实现。

下面是一个按钮样式(负责背景颜色更改)和模板(没有默认按钮行为)的示例,您可以重复使用它,还可以通过ContentPresenter更改内容:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <Style x:Key="ColorfulButtonStyle" TargetType="Button">
                    <Style.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="Pink"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="False">
                            <Setter Property="Background" Value="Blue"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Purple"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="False">
                            <Setter Property="Background" Value="green"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
                <ControlTemplate x:Key="EmptyButtonTemplate" TargetType="Button">
                    <Border Background="{TemplateBinding Background}" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

    <Button Template="{StaticResource EmptyButtonTemplate}" Style="{StaticResource ColorfulButtonStyle}" Width="30" Height="30" >
        <Ellipse HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="Black"/>
        <!--Replace the ellipse with the shape you like, defined as a control in code or in xaml-->
    </Button>

1
投票

下面的代码是在统一了两个UserControls之后。

我的XAML:

<UserControl x:Class="cs_wpf_test_1.ArrowButton"
             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" 
             xmlns:local="clr-namespace:cs_wpf_test_1"
             mc:Ignorable="d" 
             d:DesignHeight="120" d:DesignWidth="175"
             Loaded="UserControl_Loaded">
    <Button Name="MyButton" Focusable="False" Padding="0,0,0,0">
        <Button.Resources>
            <Style x:Key="styleWithPlusSign">
                <Style.Triggers>
                    <Trigger Property="Grid.Row" Value="0">
                        <Setter Property="Path.Data" Value="M 5,95 L 95,95 50,5 5,95"></Setter>
                    </Trigger>
                    <Trigger Property="Grid.Row" Value="1">
                        <Setter Property="Path.Data" Value="M 50,10 L 50,10 L 50,90 M 10,50 L 90,50"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="styleWithMinusSign">
                <Style.Triggers>
                    <Trigger Property="Grid.Row" Value="0">
                        <Setter  Property="Path.Data" Value="M 5,5 L 50,50 95,5 5,5"></Setter>
                    </Trigger>
                    <Trigger Property="Grid.Row" Value="1">
                        <Setter Property="Path.Data" Value="M 10,50 L 90,50"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Resources>
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}"
                         BorderBrush="{TemplateBinding BorderBrush}"
                         BorderThickness="{TemplateBinding BorderThickness}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates" CurrentStateChanged="CommonStates_CurrentStateChanged">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver" />
                            <VisualState x:Name="Pressed" />
                            <VisualState x:Name="Disabled" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Path Stroke="Blue"
                              Stretch="Fill"
                              x:Name="MyFirstPath"
                              Style="{StaticResource styleWithPlusSign}"
                              Grid.Row="0"/>
                        <Path Stroke="Black"
                              StrokeThickness="1"
                              Stretch="Uniform"
                              x:Name="MySecondPath"
                              Style="{Binding ElementName=MyFirstPath, Path=Style}"
                              Grid.Row="1"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Button.Template>
    </Button>
</UserControl>

我的代码隐藏:

/// <summary>
/// Interaction logic for ArrowButton.xaml
/// </summary>
public partial class ArrowButton : UserControl
{
    internal Path MyTemplateSecondPath,
        MyTemplateFirstPath;

    public ArrowButton()
    {
        InitializeComponent();

        // TODO: use smth like MyButton.PropertyChanged to set this:
        MyButton.Margin = new Thickness(
            -MyButton.BorderThickness.Left,
            -MyButton.BorderThickness.Top,
            -MyButton.BorderThickness.Right,
            -MyButton.BorderThickness.Bottom);
    }

    public bool State
    {
        get
        {
            return (bool)GetValue(StateProperty);
        }
        set
        {
            SetValue(StateProperty, value);
        }
    }
    public static readonly DependencyProperty StateProperty =
        DependencyProperty.Register("State", typeof(bool),
            typeof(ArrowButton), new PropertyMetadata(true, new PropertyChangedCallback(OnStateChanged)));

    private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = d as ArrowButton;

        b.MyButton.ApplyTemplate();

        b.MyTemplateFirstPath = (Path)b.MyButton.Template.FindName("MyFirstPath", b.MyButton);

        if (b.State)
        {
            // plus
            b.MyTemplateFirstPath.Style = b.MyButton.FindResource("styleWithPlusSign") as Style;
        }
        else
        {
            // minus
            b.MyTemplateFirstPath.Style = b.MyButton.FindResource("styleWithMinusSign") as Style;
        }
    }

    internal void SetPseudofocused(bool p)
    {
        if (p)
        {
            BorderBrush = Brushes.Blue;
        }
        else
        {
            BorderBrush = Brushes.Transparent;
        }
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        ApplyTemplate();

        MyTemplateSecondPath = (Path)MyButton.Template.FindName("MySecondPath", MyButton);

        UpdateMyLayout();
    }

    private void CommonStates_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
    {
        var btn = e.Control as Button;

        if (e.NewState.Name == "MouseOver")
        {
            btn.Background = Brushes.White;
        }
        else if (e.NewState.Name == "Pressed")
        {
            btn.Background = Brushes.LightBlue;
        }
        else if (e.NewState.Name == "Disabled")
        {
            btn.Background = Brushes.Gray;
        }
        else
        {
            btn.Background = (Brush)MyButton.FindResource(SystemColors.ControlBrushKey);
        }
    }

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);

        if (e.Property == WidthProperty ||
            e.Property == HeightProperty ||
            e.Property == BorderThicknessProperty)
        {
            UpdateMyLayout();
        }
    }

    internal void UpdateMyLayout()
    {
        if (MyTemplateSecondPath == null)
        {
            return;
        }

        // update focus border size:
        double v = ActualHeight / 25d;
        BorderThickness = new Thickness(v, v, v, v);

        double min = Math.Min(ActualWidth, ActualHeight);

        if (State)
        {
            MyTemplateSecondPath.Margin = new Thickness(
                min / 5,
                min / 5,
                min / 5,
                min / 5);
        }
        else
        {
            MyTemplateSecondPath.Margin = new Thickness(
                min / 2.2,
                min / 2.2,
                min / 2.2,
                min / 2.2);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.