0

不太确定我是否以错误的方式接近这个问题,看不到解决方案,或者只是达到了 wpf 引擎的限制。我有一个用户控件,其中有一个网格,我正在尝试DisplayMemberBinding从调用它的控件中设置控件内的网格​​视图:

UserControl:(为简洁起见)

<UserControl x:Class="MyAssembly.UserControls.IncludedItems">
    <Grid>
        <ListView x:Name="lstViewItems" Grid.Row="0" ItemsSource="{Binding AffectedFiles, FallbackValue={x:Null}}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="90">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <CheckBox Margin="5,2" IsChecked="{Binding Path=DataContext.AllSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, FallbackValue=false}" />
                                    <TextBlock Text="Include?" />
                                </DockPanel>
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid Width="90">
                                    <CheckBox HorizontalAlignment="Center" IsChecked="{Binding ShouldInclude}" />
                                </Grid>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn
                        x:Name="gvcDisplayPath"
                        Header="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=ColumnHeader, FallbackValue=Path}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</UserControl>

所以在gvcDisplayPath我希望能够从调用控件中设置 DisplayMemberPath:

<Window x:localControls="clr-namespace:MyAssembly.UserControls">
    <Grid>
        ...
        <localControls:IncludedItems DataContext="{Binding FallbackValue={StaticResource vmDesignModel}}" DisplayMemberPath="Catalog" ColumnHeader="Site" />
        ...
    </Grid>
</Window>

我确实尝试根据依赖属性在控件、ctr 和OnInitialised方法的代码隐藏中进行设置,但这不起作用(因为 dp 那时还没有设置为它的值)。

谢谢

4

1 回答 1

0

我的最终解决方案是在用户控件的 DependencyProperty 注册中挂钩PropertyChangedCallback,并从那里更新绑定(为简洁起见):

public partial class IncludedItems : UserControl {

    public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(TargetedItemView), new UIPropertyMetadata("Item", DisplayMemberPathPropertyChanged));

    private static void DisplayMemberPathPropertyChanged(object sender, DependencyPropertyChangedEventArgs e) {
        IncludedItems view = sender as IncludedItems;
        if (null == view)
            return;

        view.gvcDisplayPath.DisplayMemberBinding = new Binding(view.DisplayMemberPath) { TargetNullValue = String.Empty, FallbackValue = String.Empty };
    }

}
于 2013-08-28T08:57:45.943 回答