Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

DataTemplate And Update Binding Explictly Problem

$
0
0

I am using a DataTemplateSelector to swap different input methods for user based on whether he wants text or date. Which means the selector switches between a TextBox and a DatePicker. Each control once selector selects the desired template must use explicit way to update binding source.

To sum up the user could pick a date or enter a text and once he is done he can click on Ok Button which shall trigger the source which is a viemodel property of type object to get updated. It has to be explicit way of updating the source since i cannot count on lostfocus and i also cannot use propertychanged.

The owner control of the DataTemplateSelector is a custom ContentControl called InputControl which is futhermore part of a UserControl.

Here is a small piece of pseudocode just to visualize things better:

public class InputControl : ContentControl
{
  Public void Update()
  {
  }
}

Xaml looks kinda like this:

<UserControl><UserControl.Resources><DataTemplatex:key="text"><TextBoxText="{Binding Mode=TwoWay, Path=., UpdateSourceTrigger=Explicit}"/></DataTemplate><DataTemplatex:key="date"><DatePickerDateValue="{Binding Mode=TwoWay, Path=., UpdateSourceTrigger=Explicit}"/></DataTemplate><MyDataTemplateSelectorx:key="myDataTemplateSelector"TextTemplate="{StaticResource text}"DateTemplate="{StaticResource date}"></MyDataTemplateSelector></UserControl.Resources><InputcontrolContent="{Binding Path=., Mode=TwoWay}"ContentTemplateSelector="{StaticResource myDataTemplateSelector}"/></UserControl>

The selector looks like this

Public class MyDataTemplateSelector : DataTemplateSelector
{
  Public DataTemplate TextTemplate { get; set;}

  Public DataTemplate  DateTemplate { get; set;}

  Public DataTDemplate Select(.....)
  {
    ....
  }
}

Now the problem is how do I update the binding source from InputControl no matter what control is selected inside the template? If its TextBox selected the user shall be able to just call InputControl.Update() and it will update binding source. If its DatePicker the user shall be able to do the same which is only to call InputControl.Update() and he doesnt need to care. The source will get updated.

To sum up the method Update inside InputControl class is the central method that updates the binding source no matter if its TextBox or DatePicker. How do I do that?

Please dont forget all the requirements I have while trying to suggest me the solutions. The update source must be done explictly and Inputcontrol class has the method update which updates the source no matter what template the user selected.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>