1

次のサンプルは正常に動作しているように見えますが、出力ウィンドウに多数のバインディング エラーが生成されます。出力ウィンドウを頻繁に使用し、これらのエラーで散らかしたくないので、どうすればそれらを解決できますか。

public partial class Window1 : Window
{
    public class Item
    {
        public Color Colour { get; set; }
        public double Thickness { get; set; }
    }

    public ObservableCollection<Item> Items { get; private set; }

    public Window1()
    {
        Items = new ObservableCollection<Item>();
        Items.Add(new Item() { Colour = Colors.Red, Thickness = 1 });
        Items.Add(new Item() { Colour = Colors.Green, Thickness = 2 });
        Items.Add(new Item() { Colour = Colors.Blue, Thickness = 3 });

        DataContext = this;
        InitializeComponent();
    }

    protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseDoubleClick(e);

        if(Items.Count > 0)
            Items.RemoveAt(Items.Count-1);
    }
}
<Window x:Class="WpfApplication67.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ContentControl>
    <ContentControl.Template>
        <ControlTemplate>
            <Border Name="b">
                <ItemsControl ItemsSource="{Binding Items}" DisplayMemberPath="Colour"/>
            </Border>
           
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding Items.Count}" Value="0">
                    <Setter TargetName="b" Property="BorderBrush" Value="Red"/>
                    <Setter TargetName="b" Property="BorderThickness" Value="8"/>
                </DataTrigger>

                <DataTrigger Binding="{Binding Items.Count}" Value="1">
                    <Setter TargetName="b" Property="BorderBrush">
                        <Setter.Value>
                            <SolidColorBrush Color="{Binding Items[0].Colour}"/>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="b" Property="BorderThickness" Value="{Binding Items[0].Thickness}"/>
                </DataTrigger>
                
                <DataTrigger Binding="{Binding Items.Count}" Value="2">
                    <Setter TargetName="b" Property="BorderBrush">
                        <Setter.Value>
                            <SolidColorBrush Color="{Binding Items[1].Colour}"/>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="b" Property="BorderThickness" Value="{Binding Items[1].Thickness}"/>
                </DataTrigger>
                
                <DataTrigger Binding="{Binding Items.Count}" Value="3">
                    <Setter TargetName="b" Property="BorderBrush">
                        <Setter.Value>
                            <SolidColorBrush Color="{Binding Items[2].Colour}"/>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="b" Property="BorderThickness" Value="{Binding Items[2].Thickness}"/>
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

アプリケーションを起動すると、次のエラーが表示されます

System.Windows.Data エラー: 2: ターゲット要素の管理 FrameworkElement または FrameworkContentElement が見つかりません。BindingExpression:Path=Items[2].Colour; DataItem='Window1' (Name=''); ターゲット要素は 'SolidColorBrush' (HashCode=47633461) です。ターゲット プロパティは 'Color' (タイプ 'Color')

System.Windows.Data エラー: 2: ターゲット要素の管理 FrameworkElement または FrameworkContentElement が見つかりません。BindingExpression:Path=Items[0].Colour; DataItem=null; ターゲット要素は 'SolidColorBrush' (HashCode=45523402) です。ターゲット プロパティは 'Color' (タイプ 'Color')

System.Windows.Data エラー: 2: ターゲット要素の管理 FrameworkElement または FrameworkContentElement が見つかりません。BindingExpression:Path=Items[1].Colour; DataItem=null; ターゲット要素は 'SolidColorBrush' (HashCode=35287174) です。ターゲット プロパティは 'Color' (タイプ 'Color')

System.Windows.Data エラー: 2: ターゲット要素の管理 FrameworkElement または FrameworkContentElement が見つかりません。BindingExpression:Path=Items[2].Colour; DataItem=null; ターゲット要素は 'SolidColorBrush' (HashCode=44419000) です。ターゲット プロパティは 'Color' (タイプ 'Color')

そして、クリックしてアイテムを削除すると、

System.Windows.Data エラー: 16 : 'Items' (タイプ 'ObservableCollection`1') から 'Item[]' 値 (タイプ 'Item') を取得できません。BindingExpression:Path=Items[2].Thickness; DataItem='Window1' (Name=''); ターゲット要素は 'Border' (Name='b'); ターゲット プロパティは 'BorderThickness' (タイプ 'Thickness') です TargetInvocationException:'System.Reflection.TargetInvocationException: 呼び出しのターゲットによって例外がスローされました。---> System.ArgumentOutOfRangeException: インデックスが範囲外でした。負ではなく、コレクションのサイズより小さくなければなりません。

4

1 に答える 1