سؤال

So am testing multibinding in wpf and i have three text boxes which should get the year,month,day and my converter class should return a date with those inputs..pretty simple.

But in my convert method the values[0] is always unset that is i am always getting Dependencyproperty.UnsetValue even if get give it an initial value.

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

    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();
    }
}

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top