2

我需要将我的 ComboBox 绑定到来自 UserControl 的 DataContext 的“Rayons”属性(而不是包含我的 ComboBox 的 ListView 的 ItemSources)。

我尝试使用 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 查看是否存在绑定错误。

nevertheless if you use RelativeSource binding your Path should alomost be something like: Path=DataContext.Rayons. in your case the binding need a Rayons property on your usercontrol, but there is of course no Rayons property.

the relativesource binding like you did works most time, but when you have a usercontrol in a usercontrol in a usercontrol it becomes difficult ;) is such cases i use DataContextMarkerInterface(empty interfaces).

public interface IDataContextMarkerRayonsSource {}

then add this interface to your specific usercontrol and change the relativesource binding to 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 回答