0

所以我在 wpf 中测试多重绑定,我有三个文本框,它们应该得到年、月、日,我的转换器类应该返回带有这些输入的日期。非常简单。

但是在我的转换方法中,即使 get 给它一个初始值values[0],我也总是得到未设置的值。Dependencyproperty.UnsetValue

XAML

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    xmlns:src="clr-namespace:WpfApplication2"
    Title="MultiBinding Demo" Width="200" Height="200">
<Window.Resources>
    <src:DateConverter x:Key="myConverter" />
</Window.Resources>
<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>

    </StackPanel.Resources>
    <TextBox Name="tb1" Margin="10"  Width="Auto" Height="20"></TextBox>
    <TextBox Name="tb2" Margin="10" Width="20" Height="20" ></TextBox>
    <TextBox Name="tb3" Width="20" Height="20" ></TextBox>

    <Label Name="Date" Width="50" Height="25" Margin="5" >
        <Label.Content>
            <MultiBinding Converter="{StaticResource myConverter}" Mode="OneWay">
                <Binding ElementName="tbl" Path="Text" />
                <Binding ElementName="tb2" Path="Text" />
                <Binding ElementName="tb3" Path="Text" />
            </MultiBinding>
        </Label.Content>
    </Label>
</StackPanel>

日期转换器类

    class DateConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue || values[2] == DependencyProperty.UnsetValue)
        {
           return "";
        }
        else
        {
            int year = (int)values[0];
            int month = (int)values[1];
            int day = (int)values[2];
            DateTime date = new DateTime(year, month, day);
            return date.ToShortDateString();
        }

    }

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

1 回答 1

2

我正在查看您的 XAML,看起来您的第一个 TextBox 已命名tb1(编号 1),但在绑定中您引用的是元素名称tbl(字母 L)。

于 2011-09-13T18:15:22.870 回答