【发布时间】:2013-08-28 16:57:45
【问题描述】:
不太确定我是否以错误的方式处理这个问题,看不到解决方案,或者只是达到了 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 没有设置为值)。
谢谢
【问题讨论】:
-
尝试在依赖属性的更改处理程序中设置它? (假设您将 DisplayMemberPath DP 添加到 IncludedItems 控件
-
您的
gvcDisplayPath控件是GridViewColumn对象...GridViewColumn对象上没有DisplayMemberPath属性。请考虑一下您想要什么,并用清晰的描述编辑您的问题。 -
@NicholasW 谢谢!知道我遗漏了什么,我会在几秒钟内发布我的解决方案
-
@Sheridan DisplayMemberPath 是我的 UserControl 类上的一个 DP,我试图让
gvcDisplayPath使用它