Can't access datacontext in multivalueconverter

我怕爱的太早我们不能终老 提交于 2019-12-24 04:55:07

问题


I have a usercontrol which I need to set a specific DataContext on. The usercontrol uses multivalueconverters. However, the binding in multivalueconverter fails to use the datacontext. It works fine for regular value converters. Anyone know what is going on? To clarify, below code is not similar to my real code, it's just a sample reproducing my issue. FOr example, my multivalueconverters take more than one argument.

XAML:

<Window x:Class="MultConvTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MultConvTest="clr-namespace:MultConvTest"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <MultConvTest:MyValueConverter x:Key="MyValueConverter" />
        <MultConvTest:MyMultiConverter x:Key="MyMultiConverter" />
    </Window.Resources>

    <StackPanel>

        <!--This works, output is Formatted: 7-->
        <TextBlock 
            DataContext="{Binding Path=Data}"
            Text="{Binding Path=Length, Converter={StaticResource MyValueConverter}}" />

        <!--This works, output is Formatted: 7-->
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource MyMultiConverter}">
                    <Binding Path="Data.Length" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>

        <!--This fails, the converter never access the DataContext of the textbox-->
        <TextBlock 
            DataContext="{Binding Path=Data}">
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource MyMultiConverter}">
                    <Binding Path="Length" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>

    </StackPanel>

</Window>

Code behind:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;

namespace MultConvTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            DataContext = this;
        }

        public string Data
        {
            get { return "My Text"; }
        }
    }

    public class MyValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int number = (int)value;
            return string.Format("Formatted: {0}", number);
        }

        public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class MyMultiConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // This will fail when DataContext is set. values[0] is of type Ms.Internal.NamedObject 
            // with value DependencyProperty.UnsetValue.
            int number = (int)values[0];
            return string.Format("Formatted: {0}", number);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

回答1:


You are not going to like this ;-)

Simply put the line DataContext=this before InitializeComponent()

It seems the MultiConverter for the third textbox is triggered before the global DataContext is set, thus in effect producing a null reference error...



来源:https://stackoverflow.com/questions/3701732/cant-access-datacontext-in-multivalueconverter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!