0

それで、wpfでマルチバインディングをテストしています。年、月、日を取得する必要がある3つのテキストボックスがあり、コンバータークラスはこれらの入力で日付を返す必要があります..非常に単純です。

しかし、私の convert メソッドで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>

DATECONVERTER クラス

    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 に答える