0

私の問題は、DataGrid の ComboBox の ItemsSource がバインドされていないことです。私のユーザーコントロール:

<UserControl x:Class="MyClass"
             x:Name="Root"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
             mc:Ignorable="d" 
             d:DesignHeight="360" d:DesignWidth="757">

データグリッド:

  <DataGrid x:Name="Article" 
              AutoGenerateColumns="False"
              SelectionUnit="FullRow" 
              SelectionMode="Single"
              CanUserAddRows="False" 
              CanUserDeleteRows="True"
              Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4"
              Margin="5">
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="*"
                                    Header="Article">
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding FilteredArticle, ElementName=Root}"                                      IsEditable="True" 
                                  cal:Message.Attach="[Event KeyUp] = [Action FindArticlel($source)]" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

ElementName=Root と言わなければ、彼は FilteredArticle を Article クラスにバインドしようとします。ElementName=Root と言うと、彼は実行時に次のように言います。

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Root'. BindingExpression:Path=FilteredArticle; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

FilteredArticle は、ViewModel の BindableCollection です。他のすべてのバインディングは機能しています。ここで何が問題なのですか?Caliburn.Micro の最新の安定バージョンを使用します。

4

1 に答える 1

4

経由でビューにバインドすることElementNameは、通常、悪い習慣です。主な理由は、ビューのインスタンスを作成する誰かがビューに別のインスタンスを与える可能性がx:Nameあり、それが内部バインディングを壊してしまうためです。

さらに、FilteredArticleプロパティはビューのプロパティではなく、ビューのデータコンテキストであるビューモデルのプロパティです。

これらのシナリオでは、バインディングに相対ソースを使用します

ItemsSource="{Binding DataContext.FilteredArticle, 
                      RelativeSource={RelativeSource FindAncestor, 
                                             AncestorType={x:Type UserControl}}}"

より具体的な表記法を使用できます(ただし、99%の場合は必要ありません)

ItemsSource="{Binding DataContext.FilteredArticle, 
                      RelativeSource={RelativeSource FindAncestor, 
                                             AncestorType={x:Type local:MyClass}}}"

の名前空間はどこにありlocalますかxmlnsMyClass

于 2012-05-30T09:21:32.357 に答える