Skip to main content
Clarified the problem description; added 5 characters in body
Source Link
jpsstavares
  • 3.4k
  • 6
  • 27
  • 32

I have a window named ParameterViewParameterEditorView andwith a ParameterEditorViewModel as DataContext. In the ParameterEditorViewModel I have a list of ParameterViewModel, and. 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 Parameter viewParameterView (more on that later). In the window I have a list of ParameterViewModelsResources and insection of the ResourceDictionaryParameterEditorView I add the DataTemplate:

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

I then bind an ItemsControl.ItemSource to the List of ParameterViewModels The problem is: HowSo, how can I pass a reference of the ParameterView that is created to show the ParameterViewModel in this scenarioto 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

I have a ParameterView and ParameterViewModel, and I need the ParameterViewModel to have a reference to the Parameter view (more on that later). In the window I have a list of ParameterViewModels and in the ResourceDictionary I add the DataTemplate:

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

I then bind an ItemsControl.ItemSource to the List of ParameterViewModels The problem is: How can I pass the ParameterView to the ParameterViewModel in this scenario?

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

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

Source Link
jpsstavares
  • 3.4k
  • 6
  • 27
  • 32

Pass view to viewmodel with datatemplate

I have a ParameterView and ParameterViewModel, and I need the ParameterViewModel to have a reference to the Parameter view (more on that later). In the window I have a list of ParameterViewModels and in the ResourceDictionary I add the DataTemplate:

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

I then bind an ItemsControl.ItemSource to the List of ParameterViewModels The problem is: How can I pass the ParameterView to the ParameterViewModel in this scenario?

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