MVVM Too many properties

I am doing a WPF project and trying to stick with the MVVM pattern. It has about 20 UserControls, each of which contains about 10 controls. I want to be able to change properties. For each of them I need to change Visibilityand IsEnabled(properties of the Framework element), and then change the content and text. These are at least 3 properties for each control. In all UserControls, which is 600 properties ...

I played with the idea of ​​creating a class ControlProperties, and each control was bound to the correct instance member variable. (For instance)

//Code-behind
public class ControlProperties
{
    private bool m_isEnabled;
    public property IsEnabled
    {
        get { return m_isEnabled; }
        set { m_isEnabled = value; notifyPropertyChanged("IsEnabled"); }
    }

    ControlProperties() { m_isEnabled = false; }
}

public ControlProperties controlOne;


//XAML
<Button IsEnabled={Binding controlOne.IsEnabled}/>

2+ - / , ? ( "", ). , . ... .

, , - -.

+4
1

2+ - / , ?

, , , : https://msdn.microsoft.com/en-us/library/ms749011(v=vs.110).aspx

:

namespace WpfApplication1
{
    public class SharedProperties
    {
        public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
            "IsEnabled",
            typeof(bool),
            typeof(SharedProperties),
            new FrameworkPropertyMetadata(false));

        public static void SetIsEnabled(UIElement element, bool value)
        {
            element.SetValue(IsEnabledProperty, value);
        }
        public static bool GetIsEnabled(UIElement element)
        {
            return (bool)element.GetValue(IsEnabledProperty);
        }
    }
}

... UIElement:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="Window21" Height="300" Width="300">
    <StackPanel>
        <UserControl local:SharedProperties.IsEnabled="True" />

        <Button x:Name="btn" local:SharedProperties.IsEnabled="{Binding SomeSourceProperty}" />
    ...

//get:
bool isEnabled = SharedProperties.GetIsEnabled(btn);
//set:
SharedProperties.SetIsEnabled(btn, true);
0

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


All Articles