D
D
Dmitry Shalimov2014-07-27 08:52:42
User interface
Dmitry Shalimov, 2014-07-27 08:52:42

How to create custom element in visual stidio?

When working with projects like wpf, a lot of freedom is given to create an interface. Quite often, and rather even always, the interface consists of the same type of elements such as a button, a text field, a picture. But sometimes there is a need for frequent use of more complex structures, which are extremely cumbersome. For example, an analogue of the list of checkboxes, or a sticky slider button. Is it possible to create an element that will be called by more compact code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2014-07-28
@Sumor

To do this, you can create a UserControl or a CustomControl.
In the first case, you create a UserControl (user control) by adding it to the project as an application window. You set it up as a part of the application window by adding the necessary controls to it and describing the logic in the code. After compiling the project, the control automatically gets to the toolbar.
For example:

<UserControl x:Class="WpfApplication3.UserControl1"
             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" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBox />
        <ListBox Grid.Row="1" />
    </Grid>
</UserControl>

In the second case, you sort of inherit from the base control and add decoration and some logic to the theme. In the Add Items menu, this type is called CustomControl or custom control.
Or via C#:
public MyTextBox : TextBox
{

}

Or via Xaml:
<TextBox x:Class="WpfApplication1.MyTextBox"
             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" >
   
</TextBox>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question