locked
WPF UserControl reuse RRS feed

  • Question

  • I have a FileExportUserControl; I want to reuse it on each of my TabUserControls. Each tab control binds to a custom ViewModel each model inherits from a base. One particular tab requires a FileExportUserControl to customization the bindings of ItemsSource and SelectedItem on a combobox containing a listing of FileFormats enumerations to export. 

    I created two enum binding providers in the resouces which contain the binding providers for the combobox.  In order two reuse this FileExportUserControl, I added Dependency Properties to the FileExportControl. This allowed me on the TabUserControl to specify the FileExportUserControl.FileFormatItemsSource  key value and FileExportUserControl.SelectedItem key value. 

    In the code behind of FileExportUserControl I located the combobox control, look up the provider resource by those keys and create a new bindings for the combobox.

    This worked fine, until I hit code review. Which the policy is no code behind is allowed.

    Question how can I achieve this in WPF 4.0 XAML?

    Is there a binding provider selector? Can I communicate to it via a tag or something?

    Tuesday, October 8, 2013 6:46 PM

Answers

  • Hi obas,

    >>In order two reuse this FileExportUserControl, I added Dependency Properties to the FileExportControl. This allowed me on the TabUserControl to specify the FileExportUserControl.FileFormatItemsSource  key value and FileExportUserControl.SelectedItem key value.

    For your repuirement, I think we can got this to work simply by binding, for example, we can bind the SelectedItem to a property "currentSelect" on my VM. When the value is changed, you know the user selected a new record in the master grid. Bind your detail control to the same property (if it contains all the details you need) and you are home.

    In XAML:

    SelectedItem="{Binding currentSelect, Mode=TwoWay}"...

    In ViewModel:

    private Widget _currentSelect;
    public Widget currentSelect
    {
            get { return _currentSelect; }
            set
            {
                _currentSelect = value;
                NotifyPropertyChanged("currentSelect");
            }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 9, 2013 12:03 PM

All replies

  • Hi obas,

    >>In order two reuse this FileExportUserControl, I added Dependency Properties to the FileExportControl. This allowed me on the TabUserControl to specify the FileExportUserControl.FileFormatItemsSource  key value and FileExportUserControl.SelectedItem key value.

    For your repuirement, I think we can got this to work simply by binding, for example, we can bind the SelectedItem to a property "currentSelect" on my VM. When the value is changed, you know the user selected a new record in the master grid. Bind your detail control to the same property (if it contains all the details you need) and you are home.

    In XAML:

    SelectedItem="{Binding currentSelect, Mode=TwoWay}"...

    In ViewModel:

    private Widget _currentSelect;
    public Widget currentSelect
    {
            get { return _currentSelect; }
            set
            {
                _currentSelect = value;
                NotifyPropertyChanged("currentSelect");
            }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 9, 2013 12:03 PM
  • Hi,

    Since we haven't heard from you for a long time, I temporarily close this case. I mark useful reply as answer. If you have any concerns, please free feel to reopen it or submit a new question.

    Thanks for your understanding.

    Best regards,
    Franklin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 15, 2013 6:07 AM