简体   繁体   English

WPF多绑定到视图模型

[英]WPF Multibinding to View Model

I am trying to multibind a formatted double value to a text box. 我正在尝试将格式化的double值多重绑定到文本框。 I have a converter which takes in a double and a Formatter object and returns a formatted string to be displayed. 我有一个转换器,它接受一个double和一个Formatter对象,并返回要显示的格式化字符串。 The double is bound to a particular data source and the formatter is a property in the view model. 将double绑定到特定的数据源,并且格式化程序是视图模型中的属性。 The problem I'm having is that I'm unable to bind to the view model property. 我遇到的问题是我无法绑定到视图模型属性。 This is my code in xaml 这是我在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> 

This is the property in the view model 这是视图模型中的属性

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

So, in my converter I'm able to pick up the value for "OpenValue" ,but the runtime is unable to find XLabelFormatterY1. 因此,在我的转换器中,我可以选择“ OpenValue”的值,但是运行时无法找到XLabelFormatterY1。 Most of the examples I have seen for multibinding bind to gui components. 我见过的大多数关于多重绑定的示例都绑定到gui组件。 I'm trying to bind to the view model and would appreciate all help. 我正在尝试绑定到视图模型,将不胜感激。

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. 如果此答案对您不起作用,请尝试在绑定中显式设置NotifyOnSourceUpdated="True" And also double check if you have set correct AncestorType as wookietomwookie says in his answer. 还要仔细检查您是否按照wookietomwookie在回答中所说的设置了正确的AncestorType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM