使用数据模板将视图传递给视图模型

发布于 2024-09-02 01:28:15 字数 1405 浏览 5 评论 0原文

我有一个名为 ParameterEditorView 的窗口,其中 ParameterEditorViewModel 作为 DataContext。在 ParameterEditorViewModel 中,我有一个 ParameterViewModel 列表。在 ParameterEditorView 中,我有一个 ItemsControl,其 ItemsSource 绑定到 ParameterEditorViewModel 中的 ParameterViewModel 列表。我需要 ParameterViewModel 来引用 ParameterView(稍后会详细介绍)。在 ParameterEditorViewResources 部分中,我添加 DataTemplate

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

那么,如何传递 ParameterView 的引用> 创建它是为了向其显示 ParameterViewModel 吗?

我需要 ParameterViewModel 中的 ParameterView 的原因如下: 我有一个 TextBox,其 Text 属性绑定到 PropertyModelView.Name 属性。但我想在 Name 为空或 Null 时显示默认字符串。我尝试将属性值设置为发生这种情况时所需的默认字符串,但在这种情况下未设置 TextBox.Text 。我做了这样的事情:

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

我还尝试将 TextBox.Text 绑定模式专门设置为 TwoWay,但没有成功。 我认为这是一种防止无限循环发生的防御机制,但我不确定。 在这方面的任何帮助也将受到高度赞赏。

谢谢, 何塞·塔瓦雷斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮湿 2024-09-09 01:28:15

{Binding } 有一个 FallbackValue,顺便说一句。< /a>

你的问题让我很困惑。我假设您的 PVM 有一个 PV 集合作为公共属性,它绑定在 UI 内。另外,我认为你混淆了术语。它的模型-视图-视图模型,其中视图模型是视图的 DataContext,模型由视图模型通过公共属性公开。听起来好像如果您将窗口绑定到 ViewModel 集合,它们实际上是模型。这可能看起来很迂腐,但正确使用术语将有助于你研究和提出问题。

另一个解决方案是将转换器与 FallbackValue 结合添加到您的绑定中(我不得不这样做,IIRC)。该转换器将是一个 IValueConverter,如果字符串为 null 或空,则返回“DependencyProperty.UnsetValue”。我认为这有时会起作用,因为如果 TB 为空,则 TextBox 会将绑定属性设置为空字符串而不是 null。这里有一个小例子来激发你的口哨(不保证有效;你需要调试它并调整它):

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
    }
}

{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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文