What are your “best practices” for writing custom controls in WPF

I started writing some user controls for a visual project. I was wondering what are your "best practices" when encoding custom WPF controls?

+3
source share
5 answers

Make sure the control can be reinstalled and the template changed without changing the control method. Do not take control, believing that Listbox and Button are on the same panel or that there is even a Listbox or Button. See the MSDN authoring article for some guidelines on how to do this.

+2
source

Keep property names the same as property names of inline controls, if you can do this without changing their values.

eg. if you have a custom CustomerDisplayer element, do not call the customer list of customers, call it ItemsSource.

, , , , ItemsSource, Customers.

+3

, :

<CustomObject>
    Direct content example 1
</CustomObject>

<!-- or -->

<CustomObject>
    <Button Content="Direct content example 2" />
</CustomObject>

ContentPropertyAttribute, WPF, xaml, .

:

[ContentProperty("NameOfProperty")]
public class CustomObject
{
    [...]

ContentControl Content, , ; WPF TextBox, , Text.

.

[ContentProperty("Text")]

(. MSDN ).

, xaml, ContentControl , TextBox (TextBox ContentControl).

+3

ControlTemplate. TemplatePart.

Combobox, , TextBox a Popup .

, :

[TemplatePart(name="PART_EditableTextBox", type=typeof(TextBox))]
[TemplatePart(name="PART_Popup", type=typeof(Popup))]
public class Combobox : Selector
{
    [...]

- "PART_controlIdentifier".

, OnApplyTemplate.

, , .

, , ( , Expression Blend), , .

0

, ( ), .

, WPF.

, :

  • , .

  • , , .

0

Source: https://habr.com/ru/post/1715610/


All Articles