2

ComboBox を (ComboBox を含む ListView の ItemSources ではなく) UserControl から DataContext の 'Rayons' プロパティにバインドする必要があります。

RelativeSource を使用しようとしましたが、機能せず、デバッグ ウィンドウにエラー メッセージが表示されません。

簡略化されたコード:

<UserControl xmlns:my="clr-UI.View"
             x:Class="UI.View.MontureView"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
             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" 
             mc:Ignorable="d"
             d:DesignHeight="673" d:DesignWidth="980">
        <ListView ItemsSource="{Binding Path=Monture, Mode=TwoWay}" Margin="0,39,0,95" Height="600" HorizontalAlignment="Center">
            <ListView.View>
                <GridView>
                    <GridViewColumn >
                        <GridViewColumn.CellTemplate >
                            <DataTemplate>
                                <ComboBox Height="25" HorizontalAlignment="Center" Width="125"
                                          Name="comboBoxRay" Margin="0,2,0,3"
                                          ItemsSource="{Binding Path=Rayons,  RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                          SelectedValue="{Binding Path=IDRayon, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                          DisplayMemberPath="SRAY_LIBELLE"
                                          SelectedValuePath="SRAY_ID"  />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
</UserControl>

どうすればそのケースを処理できますか?

そのドキュメントを見つけましたが、役に立ちませんでした

4

2 に答える 2

2

これを処理する方法はいくつかあります。ただし、最初にバインド エラーが表示されない場合は、Snoop を使用してバインド エラーがあるかどうかを確認してください。

それでも、RelativeSource バインディングを使用する場合、Path はほぼ次のようになります: Path= DataContext .Rayons. あなたの場合、バインディングにはユーザーコントロールの Rayons プロパティが必要ですが、もちろん Rayons プロパティはありません。

あなたが行ったようなrelativesourceバインディングはほとんどの場合機能しますが、ユーザーコントロールのユーザーコントロールにユーザーコントロールがあると難しくなります;)そのような場合、私はDataContextMarkerInterface (空のインターフェース)を使用します。

public interface IDataContextMarkerRayonsSource {}

次に、このインターフェイスを特定のユーザー コントロールに追加し、relativesource バインディングを AncestorType に変更します。

ItemsSource="{Binding Path=DataContext.Rayons, RelativeSource={RelativeSource AncestorType={x:Type local:IDataContextMarkerRayonsSource }}}"
于 2012-07-31T08:43:06.637 に答える
1

あなたはそれを言います: のDataContextプロパティUserControl。試す

ItemsSource="{Binding Path=DataContext.Rayons,
                      RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
于 2012-07-31T08:42:44.703 に答える