WPF 多重绑定到视图模型 [英] WPF Multibinding to View Model

查看:33
本文介绍了WPF 多重绑定到视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将格式化的双精度值多重绑定到文本框.我有一个转换器,它接受一个 double 和一个 Formatter 对象并返回一个要显示的格式化字符串.double 绑定到特定数据源,格式化程序是视图模型中的一个属性.我遇到的问题是我无法绑定到视图模型属性.这是我在 xaml 中的代码

I am trying to multibind a formatted double value to a text box. I have a converter which takes in a double and a Formatter object and returns a formatted string to be displayed. The double is bound to a particular data source and the formatter is a property in the view model. The problem I'm having is that I'm unable to bind to the view model property. This is my code in xaml

    <StackPanel Grid.Row="0" Grid.Column="1">
      <TextBlock HorizontalAlignment="Left" Style="{StaticResource HintDataItemsStyle}">
        <TextBlock.Text>
         <MultiBinding Converter="{StaticResource FormatConverter}">
           <Binding Path="OpenValue" />
           <Binding Path="XLabelFormatterY1" />
         </MultiBinding>
        </TextBlock.Text>
      </TextBlock> 

这是视图模型中的属性

    private ILabelFormatter _labelFormatterY1;
    public ILabelFormatter XLabelFormatterY1
    {
        get { return _labelFormatterY1; }
        set
        {
            _labelFormatterY1 = value;
            OnPropertyChanged("XLabelFormatterY1");
        }
    }

因此,在我的转换器中,我能够获取 "OpenValue" 的值,但运行时无法找到 XLabelFormatterY1.我见过的大多数多绑定绑定到 gui 组件的例子.我正在尝试绑定到视图模型,并希望得到所有帮助.

So, in my converter I'm able to pick up the value for "OpenValue" ,but the runtime is unable to find XLabelFormatterY1. Most of the examples I have seen for multibinding bind to gui components. I'm trying to bind to the view model and would appreciate all help.

推荐答案

老问题但没有答案.我相信您正在寻找此解决方案.如果此答案对您不起作用,请尝试在绑定中显式设置 NotifyOnSourceUpdated="True".还要仔细检查您是否设置了正确的 AncestorType,正如 wookietomwookie 在他的回答中所说的那样.

Old question but without answer. I beleive that you are looking for this solution. If this answer doesn't work for you, try to explicitly set NotifyOnSourceUpdated="True" in the binding. And also double check if you have set correct AncestorType as wookietomwookie says in his answer.

这篇关于WPF 多重绑定到视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆