Pass view to viewmodel with datatemplate
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 HorizontalAli开发者_如何学Gognment="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
{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
}
}
精彩评论