不太确定我是否以错误的方式接近这个问题,看不到解决方案,或者只是达到了 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 那时还没有设置为它的值)。
谢谢