0

I have a window named ParameterEditorView with a ParameterEditorViewModel as DataContext. In the ParameterEditorViewModel I have a list of ParameterViewModel. In the ParameterEditorView I have an ItemsControl whose ItemsSource is binded to the list of ParameterViewModel in the ParameterEditorViewModel. I need the ParameterViewModel to have a reference to the ParameterView (more on that later). In the Resources section of the ParameterEditorView I add the DataTemplate:

<DataTemplate DataType="{x:Type my:ParameterViewModel}" > <my:ParameterView HorizontalAlignment="Left"/> </DataTemplate> 

So, how can I pass a reference of the ParameterView that is created to show the ParameterViewModel to it?

The reason I need the ParameterView in the ParameterViewModel is the following: I have a TextBox whose Text property is binded to the PropertyModelView.Name property. But I want to display a default string when the Name is empty or Null. I've tried to set the property value to the default string I want when that happens but the TextBox.Text is not set in this scenario. I do something like this:

private string _name; public string Name { get { return _name; } set { if (value == null || value.Length == 0) Name = _defaultName; else _name = value; } } 

I've also tried to specifically set the TextBox.Text binding mode to TwoWay without success. I think this is a defense mechanism to prevent an infinite loop from happening but I don't know for sure. Any help on this front would also be highly appreciated.

Thanks, José Tavares

1 Answer 1

1

{Binding } has a FallbackValue, btw.

Your question, it confuses me. I'd assume your PVM has a collection of PV's as a public property, which is bound within the UI. Also, I think you're mixing terms. Its Model-View-ViewModel where the ViewModel is the DataContext of the View, and the Model is exposed by the ViewModel via a public property. Sounds like if you're binding the window to a collection of ViewModels they are actually Models. It may seem pedantic, but getting your terms correct will help you research and ask questions.

Another solution would be to add a Converter to your Binding in combination with FallbackValue (I've had to do this, IIRC). That converter would be an IValueConverter that returns "DependencyProperty.UnsetValue" if the string is null or empty. I think this works sometimes because the TextBox will set the bound property to the empty string rather than null if the TB is empty. Here's a little sample to whet your whistle (not guaranteed to work; you need to debug this and tweak it):

public class ThisMightWorkConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var temp = value as string; if(string.IsNullOrWhiteSpace(temp)) return System.Windows.DependencyProperty.UnsetValue; return temp; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; // you might need to change this } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Well I may have not express myself clearly. I have a window named ParameterEditorView and a ParameterEditorViewModel. In the ParameterEditorView I have a list of ParameterViewModel. In the ParameterEditorView I have a ItemsControl whose ItemsSource is binded to the list of ParameterViewModel in the ParameterEditorViewModel. I also define the DataTemplate shown above in the ParameterEditorView. The binding is working fine, but I want to know if I can pass the reference of the ParameterView, that is automatically created to display a ParameterViewModel, to the ParameterViewModel.
@jps assuming ParameterView is a UserControl. If you're trying to pass a chunk of UI to the ViewModel or a Model you're doing something wrong. I think it would be to your benefit to state your problem and ask for solutions rather than go down the road you're on. FallbackValue is one option. I edited to add a second option.
I guess this second option is good option to overcome the problem. Thanks for the help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.