1

I have a delegate that is called from native code to tell Visibility and IsEnabled for each button. Making 22 properties like

bool Insert_BtnSaveIsEnabled {get; set; }
Visibility Insert_BtnSaveVisibility {get; set; }

bool Insert_BtnSelectObjectsIsEnabled {get; set; }
Visibility Insert_BtnSelectObjectsVisibility {get; set; }

to ViewModel and bind a pair for each button's IsEnabled and Visbility in XAML does not sound reasonable. How this should be done?

ViewModel:

    private int ControlsStateChange(string control, CommonDllInterface.FieldStateSet state)
    {
        //here I could have huge if-else clause
        //to determine one of 22 properties to set
        //and the value to assign to it        
    }

enum:

    public enum FieldStateSet 
    {
        Undefined = 0,
        Inactive = 1,
        Active = 2,
        Visible = 3,
        Hidden = 4
    }

value Hidden is always mapped to Visibilty.Collapsed.

Here is part of the XAML (it is quite complex/long so these few buttons should do as an example):

      <Button Name="Insert_BtnSave" Command="{ui:CommandHandler CommonCommandshandler}">
            <Image Source="{Binding Converter={StaticResource nameToBitmapSource}}" DataContext="BmpSaveSettings" />
        </Button>
        <Button Name="Insert_BtnSelectObjects"  Command="{ui:CommandHandler CommonCommandshandler}">
            <Image Source="{Binding Converter={StaticResource nameToBitmapSource}}" DataContext="BmpBrwse" />
        </Button>
        <Button Name="Insert_BtnSelectPL" Command="{ui:CommandHandler CommonCommandshandler}" Visibility="{Binding Visibility}">
            <Image Source="{Binding Converter={StaticResource nameToBitmapSource}}" DataContext="BmpPlanProperties" />
        </Button>
        <Button Name="Insert_BtnCopy"  Command="{ui:CommandHandler CommonCommandshandler}" IsEnabled="{Binding IsEna}">
            <Image Source="{Binding Converter={StaticResource nameToBitmapSource}}" DataContext="BmpCopy" />
        </Button>
        <Button Name="Insert_BtnDelete" Command="{ui:CommandHandler CommonCommandshandler}">
            <Image Source="{Binding Converter={StaticResource nameToBitmapSource}}" DataContext="BmpDelete" />
        </Button>
        <Button Name="Insert_BtnEditProps"  Command="{ui:CommandHandler CommonCommandshandler}">
            <Image Source="{Binding Converter={StaticResource nameToBitmapSource}}" DataContext="BmpOfflineProperties" />
        </Button>

Names of the buttons in XAML correspond to control-parameter value in delegate.

char m
  • 7,840
  • 14
  • 68
  • 117
  • 1
    Can you please post more code and details? This is a very low quality question. It's hard to understand what you're actually asking. Thanks – DotNetRussell Jun 29 '16 at 22:46
  • i don't understand what you mean. Let's say 11 buttons in xaml. How to bind their IsEnabled and Visibility? 22 properties in ViewModel is not handy. – char m Jun 29 '16 at 22:49
  • 1
    You would bind it the same way you bind all other properties. If you post your XAML and C# then I will be happy to take a look at it – DotNetRussell Jun 29 '16 at 22:50
  • I appreciate but I'm sorry but xaml is just 11 buttons. Posting that makes no difference. My point is that I don't want to make 22 properties in ViewModel for them. This can be solved in better way. I just can't figure out how. – char m Jun 29 '16 at 22:57
  • 2
    It's not pointless. The answer is easy. I'm refusing to answer because the point is this isn't your personal blog. It's a Q&A repository. This question will be a historical record that other people with similar questions will use. If you don't post code then there is less for the search engines to index – DotNetRussell Jun 29 '16 at 23:01
  • i really don't want to argue with person wanting to help people. but the code is 11 buttons in xaml. that's it. thanks anyway. – char m Jun 29 '16 at 23:07
  • Enough of the banter. Let me ask this... are the buttons mutually exclusive only one is on when the others are on/off? Is there a pattern that some are on at same time and others off? Additionally, enabled vs visibility are different. Are you trying to show/hide AND ALSO enable/disable? – DRapp Jun 29 '16 at 23:24
  • I am trying to do both. I update the question with delegate. – char m Jun 29 '16 at 23:29
  • and no mutual exclusion or any other relation, i just have to set one control at time collapsed/visible or enabled/not enabled. – char m Jun 29 '16 at 23:38

1 Answers1

2

First of all, I assume that the below is in ViewModel:

bool Insert_BtnSaveIsEnabled {get; set; }
Visibility Insert_BtnSaveVisibility {get; set; }

You should not use Visibility in ViewModel, you should use bool in ViewModel, then use a converter in View to turn it into Visibility type. ViewModel should always be POCO.

Secondly, if you don't want to make 22 properties in ViewModel, you can combine them into 2 ObservableDictionary properties (e.g. public ObservableDictionary<bool> ButtonIsEnabled { get; set } and public ObservableDictionary<bool> ButtonIsVisible { get; set }).

If you don't want to use ObservableDictionary, you can still use the ordinary Dictionary, and manually call the OnPropertyChanged when you modify anything in the dictionaries.

The key of the dictionary is the name of the button, while the value is well.. the value.

In your View, you bind to the dictionary properties, and implement a converter for each property. You need to specify ConverterParameter (name of the button) for each button's bindings, and let the converter retrieve the value from the dictionaries.

This is probably not the most elegant way to do it, and it is definitely not a simple/easy way either. You would spend more time doing this, than doing the 22 properties in ViewModel. Personally, I would just put the 22 properties in ViewModel and wrap them with a #region, then collapse it - so that I don't see them when I work on my ViewModel.

Community
  • 1
  • 1
Jai
  • 8,165
  • 2
  • 21
  • 52
  • I was thinking something like this approach, but changing state of 1 item in collection results all buttons to retrieve their state. Am I right? – char m Jun 30 '16 at 14:13
  • i accept this answer later if there won't be more elegant way i.e. that changing one button state results only 1 get-accessor to be called (i don't even know if that's possible without 22 properties) – char m Jul 01 '16 at 04:33
  • 1
    @matti You can take a look at mediator design pattern. I can't comment much since I've never used it. Maybe it can do this job correctly. – Jai Jul 01 '16 at 05:39