-->

WPF Multibinding to View Model

2019-07-26 22:26发布

问题:

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> 

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

回答1:

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.