質問

特定のdataContextを設定する必要があるusercontrolがあります。 UserControlはMultivalueConvertersを使用します。ただし、MultivalueConverterでのバインディングは、DataContextを使用できません。通常の値コンバーターでは正常に動作します。誰もが何が起こっているのか知っていますか?明確にするために、以下のコードは私の実際のコードに似ていませんが、それは私の問題を再現するサンプルだけです。たとえば、MultivalUeConvertersは複数の引数を取ります。

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();
        }
    }
}
役に立ちましたか?

解決

あなたはこれを好きになるつもりはありません;-)

単にラインを置きます DataContext=this InitializeComponent()

3番目のテキストボックスのマルチコンバーターは、グローバルなデータコンテキストが設定される前にトリガーされるように思われます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top