代码之家  ›  专栏  ›  技术社区  ›  Wallstreet Programmer

无法访问多值转换器中的DataContext

  •  2
  • Wallstreet Programmer  · 技术社区  · 15 年前

    我有一个需要在其上设置特定数据上下文的用户控件。用户控件使用多值转换器。但是,多值转换器中的绑定无法使用DataContext。它适用于常规值转换器。有人知道发生了什么事吗?为了澄清,下面的代码与我的真实代码不相似,它只是一个复制我问题的示例。例如,我的多值转换器接受多个参数。

    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>
    

    代码隐藏:

    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 回复  |  直到 15 年前
        1
  •  4
  •   Dabblernl    15 年前

    你不会喜欢这个的;-)

    简单地说 DataContext=this 之前 InitializeComponent()

    似乎第三个文本框的多转换器是在设置全局数据上下文之前触发的,因此实际上会产生空引用错误…

    推荐文章