无法访问多值转换器中的数据上下文
我有一个用户控件,我需要在其上设置特定的 DataContext。用户控件使用多值转换器。但是,multivalueconverter 中的绑定无法使用数据上下文。它对于常规值转换器来说效果很好。有人知道发生了什么事吗?澄清一下,下面的代码与我的真实代码并不相似,它只是重现我的问题的示例。例如,我的多值转换器接受多个参数。
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();
}
}
}
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不会喜欢这个;-)
只需将行
DataContext=this
之前InitializeComponent()
看起来是第三个文本框的 MultiConverter在设置全局 DataContext 之前触发,从而实际上产生空引用错误...
You are not going to like this ;-)
Simply put the line
DataContext=this
beforeInitializeComponent()
It seems the MultiConverter for the third textbox is triggered before the global DataContext is set, thus in effect producing a null reference error...