Establish a user evaporative element



  • There's a button.

    <UserControl x:Class="WCL1.RoundButton"
                 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:WCL1"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <Button Height="{Binding ElementName=RoundButton, Path=Height}" Width="{Binding ElementName=RoundButton, Path=Wight}" VerticalAlignment="Top" HorizontalAlignment="Left">
                <Button.Template>
                    <ControlTemplate>
                        <Grid x:Name="controlLayout">
                            <Ellipse x:Name="buttonSurface" Fill="LightBlue"></Ellipse>
                            <Label x:Name="buttonCaption" VerticalAlignment="Center"
                                   HorizontalAlignment="Center"
                                   FontSize="20" Content="{Binding ElementName=RoundButton, Path=Text}"></Label>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Grid>
    </UserControl>
    

    public partial class RoundButton : UserControl
        {
            static DependencyProperty TextProperty;
            static DependencyProperty FillProperty;
           // static DependencyProperty HeightProperty;
            //static DependencyProperty FontSizeProperty;
            //static DependencyProperty FillProperty;
            //static DependencyProperty FillProperty;
            //static DependencyProperty FillProperty;
            //static DependencyProperty FillProperty;
            public RoundButton()
            {
                TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(RoundButton),null);
                FillProperty = DependencyProperty.Register("Fill", typeof(string), typeof(RoundButton), null);
                InitializeComponent();
            }
            public string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }
            public Brush Fill
            {
                get { return (Brush)GetValue(FillProperty); }
                set { SetValue(FillProperty, value); }
            }
            private static void OnContentChanged(DependencyObject sender,
                DependencyPropertyChangedEventArgs e)
            {
                RoundButton rb = (RoundButton)sender;
            }
         }
    

    I'm connecting the library to the project and I want to use this button.

    <myDll:RoundButton Height="30" Width="30"></myDll:RoundButton>
    

    But all the characteristics of a zero reaction, and Fill's actually saying it's not acceptable, and the text's already registered in the RoundButton and makes a mistake.

    How do you make this UserControl as much as the conventional Button element?

    P. S.

    It was agreed to abandon the UserControl and use the building library of management elements

    namespace WCCL1
    {
        /// <summary>
        /// Выполните шаги 1a или 1b, а затем 2, чтобы использовать этот пользовательский элемент управления в файле XAML.
        ///
        /// Шаг 1a. Использование пользовательского элемента управления в файле XAML, существующем в текущем проекте.
        /// Добавьте атрибут XmlNamespace в корневой элемент файла разметки, где он 
        /// будет использоваться:
        ///
        ///     xmlns:MyNamespace="clr-namespace:WCCL1"
        ///
        ///
        /// Шаг 1б. Использование пользовательского элемента управления в файле XAML, существующем в другом проекте.
        /// Добавьте атрибут XmlNamespace в корневой элемент файла разметки, где он 
        /// будет использоваться:
        ///
        ///     xmlns:MyNamespace="clr-namespace:WCCL1;assembly=WCCL1"
        ///
        /// Потребуется также добавить ссылку из проекта, в котором находится файл XAML,
        /// на данный проект и пересобрать во избежание ошибок компиляции:
        ///
        ///     Щелкните правой кнопкой мыши нужный проект в обозревателе решений и выберите
        ///     "Добавить ссылку"->"Проекты"->[Поиск и выбор проекта]
        ///
        ///
        /// Шаг 2)
        /// Теперь можно использовать элемент управления в файле XAML.
        ///
        ///     <MyNamespace:RoundButton/>
        ///
        /// </summary>
        public class RoundButton : Control
        {
            string dText = "RounButton";
            Brush dFill = Brushes.Blue;
            public string Text
            {
                get { return dText; }
                set { dText = value; }
            }
            public Brush Fill
            {
                get { return dFill; }
                set { dFill = value; }
            }
            static RoundButton()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(RoundButton), new FrameworkPropertyMetadata(typeof(RoundButton)));
            }
        }
    }
    

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WCCL1">
    
    &lt;Style TargetType="{x:Type local:RoundButton}"&gt;
        &lt;Setter Property="Template"&gt;
            &lt;Setter.Value&gt;
                &lt;ControlTemplate TargetType="{x:Type local:RoundButton}"&gt;
                    &lt;Grid x:Name="controlLayout"&gt;
                        &lt;Ellipse x:Name="buttonSurface" Fill="{Binding Fill}"&gt;&lt;/Ellipse&gt;
                        &lt;Label x:Name="buttonCaption" VerticalAlignment="Center"
                               HorizontalAlignment="Center"
                               FontWeight="Bold" FontSize="20" Content="{Binding Text}"&gt;&lt;/Label&gt;
                    &lt;/Grid&gt;
                &lt;/ControlTemplate&gt;
            &lt;/Setter.Value&gt;
        &lt;/Setter&gt;
    &lt;/Style&gt;
    

    </ResourceDictionary>

    How do I connect this library now and do I have it all right?



  • Apparently, you don't need anything. UserControland ordinary style with redistribution TemplateHuh.

    <Style TargetType="Button" x:Key="RoundButtonStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="controlLayout">
                        <Ellipse x:Name="buttonSurface" Fill="LightBlue"/>
                        <ContentPresenter
                            TextBlock.FontSize="20" Content="{TemplateBinding Content}"
                            HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    To apply:

    <Button Style="{StaticResource RoundButtonStyle}">
        Test
    </Button>
    

    The mistake in your code-- you're using it. ElementName=RoundButtonbut not defining the element with that name. Besides, you have to call. DependencyProperty.Register In a static designer, otherwise you'll get a mistake when you try to use the counter for the second time.


    In the new code of error:

    1. You're in. RoundButton not defined DependencyPropertyand ordinary properties. It's not right.
    2. For {Binding Fill} I need you to. {TemplateBinding Fill}and for Text♪ The rest should work on the idea.

    But if your goal is still to look like a normal button, it's best to just try to change the style (if you can, don't. Template)




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2