0

Configuration window. Lots of options. And many different configurations to display in same window:

public class BaseConfig: INotifyPropertyChanged {}

public class ConfigA: BaseConfig
{
    public SomeType OptionA { get {...} set {...}}
}

public class ConfigB: BaseConfig
{
    public SomeOtherType OptionB { get {...} set {...}}
}

Here is view model:

public class ViewModel: INotifyPropertyChanged
{
    public BaseConfig Config {get {...} set {...}}
}

And view:

<TextBox Text="{Binding Config.OptionA}" .../>
<TextBox Text="{Binding Config.OptionB}" .../>

This will work when either ConfigA or ConfigB instance is set as viewmodel's Config. But it will show binding errors for missing properties.

My concern is if this approach any good. Imagine 100 missing properties. That would be hell-loads of binding errors = huge delay before window is shown.

Questions: Is there a nice way to prevent binding errors if property is missing? Or maybe there is a better approach to implement given scenario?

I understand what it can be done by using different views to display ConfigA and ConfigB, but I'd really like to keep all bindable controls in 1 view. In my scenario there will be 20+ configs and they share some settings, some do not. At the end it will require to create an analogue of PropertyGrid (to dinamically create view) or split views too much (all way down until there are views consisting from control to edit only one specific property). This is why I'd prefer some generic approach "binding is found - bind to it, not found - skip, no problems".

Sinatr
  • 20,892
  • 15
  • 90
  • 319

1 Answers1

1

You could use a convertor. (class that inherite from IValueConverter)

your binding will look this way

<control.Resources>
        <ConfigConverter x:key="ConfigConverter" />
</control.Resources>
<TextBox Text="{Binding Config, Converter={StaticResource ConfigConverter}, ConverterParameter=ConfigA" .../>

In your converter you look by reflection the property "ConfigA" into the object bind. If the property exist you return the value otherwise you could return a default value.

Mickael Thumerel
  • 516
  • 4
  • 14