我不确定我是以错误的方式接近这个,看不到解决方案,或者只是在wpf引擎中达到了限制。我有一个UserControl,其中有一个网格,我正在尝试在控件中的网格视图中设置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没有设置)到那时它的价值)。
由于
答案 0 :(得分: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 };
}
}