如何使用相对绑定模式= FindAncestor [英] How to use relative binding Mode=FindAncestor

查看:306
本文介绍了如何使用相对绑定模式= FindAncestor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView控件,具有3列:名称,姓氏和地址。我想要所有3列具有相同的宽度。我想出的唯一方法是将每列的宽度绑定到祖先的ActualWidth,并使用转换器将此宽度除以3,如下所示:

 < GroupBox> 
< ListView Name =PeopleItemsSource ={Binding peopleList}>
< ListView.View>
< GridView>
< GridViewColumn Header =nameDisplayMemberBinding ={Binding Name}>
< GridViewColumn.Width>
< Binding Path =ActualWidthSource ={RelativeSource Mode = FindAncestor,AncestorType = {x:Type GroupBox}}Converter ={StaticResource ListViewConverter}/>
< /GridViewColumn.Width>
< GridViewColumn Header =surname...>
...
< / GridViewColumn>
< GridViewColumn Header =address...>
...
< / GridViewColumn>
< / GridViewColumn>
< / GridView>
< /ListView.View>
< / ListView>
< / GroupBox>

转换类是:

  class ListViewConverter:IValueConverter 
{
public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
{
double state =双)值;
return state / 3;
}

public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
{
return null;
}
}

但这不行。我认为也许GroupBox不是GridViewColumn.Width的祖先。
也许我错了,有一个简单的方法呢?

解决方案

你是对的, GroupBox不是祖先 - 这是因为GridViewColumn没有添加到可视化树中,因此依赖视觉树导航的绑定将无法正常工作。您可以将列绑定到静态资源:



为了实现您以后的需求,将需要相当多的自定义代码。请参阅MSDN论坛主题:



http://social.msdn.microsoft.com/forums/en-US/wpf/thread/83bd7ab9-3407-461f-a0bc-69e04870075c



这里的代码为指定列大小提供了更多选项:



http://www.codeproject.com/KB/grid/ListView_layout_manager.aspx


I have a ListView control that has 3 columns: Name, Surname and address. I want all 3 columns to have the same width. The only way I came up with was to bind each column's width to the ActualWidth of the ancestor and use a convertor to divide this width by 3 as follows:

<GroupBox>
    <ListView Name="People" ItemsSource="{Binding peopleList}">
        <ListView.View>
                <GridView >
                        <GridViewColumn Header="name" DisplayMemberBinding="{Binding Name}">
                                <GridViewColumn.Width>
                                        <Binding Path="ActualWidth" Source="{RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}}" Converter ="{StaticResource ListViewConverter}"/>
                                    </GridViewColumn.Width>
            <GridViewColumn Header="surname" ...>
                ...
            </GridViewColumn>
            <GridViewColumn Header="address" ...>
                ...
            </GridViewColumn>
                            </GridViewColumn>
                    </GridView>
            </ListView.View>
   </ListView>
</GroupBox>

The convertion class is:

class ListViewConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double state = (double)value;
        return state / 3;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

But this doesn't work. I think that maybe the GroupBox is not an ancestor of GridViewColumn.Width. Maybe I got it all wrong and there is a much simpler way to do this?

解决方案

You are correct, GroupBox is not an ancestor - this is because the GridViewColumn is not added to the visual tree, so bindings that rely on visual tree navigation will not work. You could bind your columns to a static resource instead:

To achieve what you are after will require quite a bit of custom code. See teh following MSDN forum thread:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/83bd7ab9-3407-461f-a0bc-69e04870075c

And the code here that gives much more options for specifying column sizes:

http://www.codeproject.com/KB/grid/ListView_layout_manager.aspx

这篇关于如何使用相对绑定模式= FindAncestor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆