There is 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 connect the library to the project and want to use this button

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

But all properties have zero reaction, and the value Fill says that it is not valid, and the text "text property is already registered in RoundButton" and gives an error.

How to make this UserControl as close as possible to a regular Button element?

PS

It was decided to abandon UserControl and use a custom library of controls.

 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"> <Style TargetType="{x:Type local:RoundButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:RoundButton}"> <Grid x:Name="controlLayout"> <Ellipse x:Name="buttonSurface" Fill="{Binding Fill}"></Ellipse> <Label x:Name="buttonCaption" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20" Content="{Binding Text}"></Label> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> 

How now to connect this library and am I all zabindil?

    1 answer 1

    Apparently, you don't need a UserControl , but an ordinary style with Template overriding.

     <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> 

    Apply as follows:

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

    Error in your code - you use ElementName=RoundButton , but do not define an element with that name. In addition, you must call the DependencyProperty.Register in a static constructor, otherwise you will get an error when you try to use the control a second time.


    In the new error code:

    1. You in RoundButton define not DependencyProperty , but ordinary properties. It is not right.
    2. Instead of {Binding Fill} you need {TemplateBinding Fill} , and the same for Text . The rest should work, in theory.

    But if your goal is still to look like a normal button, it is better to try simply changing the style (if you can, not even changing the Template ).

    • I decided not to use UserControl, but to make the Custom Control Library. But I still do not understand how to set everything up and connect it. - Sanych Goilo
    • And where did you define the element with the name? - Sanych Goilo
    • @SanychGoilo: Updated the answer. // It is not necessary to declare an element with a name; you can use a TemplateBinding instead. - VladD
    • How then to connect dll in xaml? - Sanych Goilo
    • @SanychGoilo: In what sense is “plug in a DLL”? Do you mean xmlns:lib="clr-namespace:WCCL1;assembly=WCCL1" ? - VladD