wpf 如何从单元格模板绑定到 gridviewcolumn 上的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11816074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:51:00  来源:igfitidea点击:

How to bind to a property on a gridviewcolumn from its celltemplate

wpftemplatesbindinggridviewcolumn

提问by Alternator

The general problem I want to solve is to have just one datatemplate for a checkbox which I can then use for many different columns in a listview (using a gridview). In all examples I've seen a seperate template is created for each binding which seems overkill to me.

我想要解决的一般问题是一个复选框只有一个数据模板,然后我可以将其用于列表视图中的许多不同列(使用网格视图)。在所有示例中,我都看到为每个绑定创建了一个单独的模板,这对我来说似乎有点矫枉过正。

I've been trying to do this by creating an attached property that the gridviewcolumn will set. Then I can simply have one datatemplate for a checkbox that binds to that attached property.

我一直在尝试通过创建 gridviewcolumn 将设置的附加属性来做到这一点。然后我可以简单地为绑定到该附加属性的复选框创建一个数据模板。

The problem I'm having is actually setting the checkbox source to the gridviewcolumn.

我遇到的问题实际上是将复选框源设置为 gridviewcolumn。

Here is the xaml:

这是xaml:

<DataTemplate x:Key="CheckBoxTemplate">
    <CheckBox IsChecked="{Binding Path=(ap:AttachedProperties.IsChecked),
        RelativeSource={RelativeSource AncestorType={x:Type GridViewColumn}}}" />
</DataTemplate>

<GridView x:Key="MyGridView">
    <GridViewColumn Header="CheckBox" CellTemplate="{StaticResource CheckBoxTemplate}" ap:AttachedProperties.IsChecked="{Binding Path=SomeValue}" />
</GridView>

P.S. The Attached property is working fine, I've attached it directly to the checkbox and used a relativesource self binding and gotten the values coming through, but when I try to bind it on the GridViewColumn I get no happiness, in fact I haven't been able to bind to the Header on the GridViewColumn either...

PS Attached 属性工作正常,我已经将它直接附加到复选框并使用了相对源自绑定并获得了通过的值,但是当我尝试将它绑定到 GridViewColumn 时,我没有得到任何快乐,事实上我没有无法绑定到 GridViewColumn 上的标题...

P.P.S I have tried other binding source expressions as well (just not the right one it seems)...

PPS 我也尝试过其他绑定源表达式(只是看起来不正确)...

Edit: Immediately after posting this it dawned on me that the GridViewColumn exists for all rows and so that is probably why this isn't working?!

编辑:发布此消息后,我立即意识到所有行都存在 GridViewColumn,所以这可能是为什么这不起作用?!

I've slept on it now and realise that doing things in this way wont work (because the GridViewColumn can only hold the value for the first row not each row as I would need it to) - however I still would have thought it possible to bind to the GridViewColumn?

我现在已经睡了,意识到以这种方式做事是行不通的(因为 GridViewColumn 只能保存第一行的值,而不是我需要的每一行) - 但是我仍然认为有可能绑定到 GridViewColumn?

Here is a xaml only example to show the behaviour:

这是显示行为的仅 xaml 示例:

<Window x:Class="WpfApplication3.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="MainWindow"
       Width="525"
       Height="350">
   <Grid>
       <ListView Name="listView1"
                 Width="479"
                 Height="287"
                 Margin="12,12,0,0"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top">
           <ListView.View>
               <GridView>
                   <GridViewColumn Header="A Header">
                       <GridViewColumn.CellTemplate>
                           <DataTemplate>
                               <StackPanel>
                                   <TextBlock Text="{Binding Path=Header, RelativeSource={RelativeSource AncestorType={x:Type GridViewColumn}}}" />
                                   <TextBlock Text="Visible row" />
                               </StackPanel>
                           </DataTemplate>
                       </GridViewColumn.CellTemplate>
                   </GridViewColumn>
               </GridView>
           </ListView.View>
           <ListViewItem />
           <ListViewItem />
       </ListView>
   </Grid>
</Window>

Under such a scenerio I would expect to see "A Header" just above each "Visible row" text

在这样的场景下,我希望在每个“可见行”文本上方看到“标题”

回答by 123 456 789 0

I've check snoop, the reason why that is not working because GridColumnHeader is not in the VisualTree where the GridRows are. My solution is to use ElementName and bind it to the Header property. Fairly simple, much cleaner and readable.

我检查了 snoop,原因是它不起作用,因为 GridColumnHeader 不在 GridRows 所在的 VisualTree 中。我的解决方案是使用 ElementName 并将其绑定到 Header 属性。相当简单,更清晰和可读。

    <ListView Name="listView1"
             Width="479"
             Height="287"
             Margin="12,12,0,0"
             HorizontalAlignment="Left"
             VerticalAlignment="Top">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="A Header" x:Name="HeaderOne">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding ElementName=HeaderOne,Path=Header}" />
                                <TextBlock Text="Visible row" />
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <ListViewItem />
        <ListViewItem />
    </ListView>

回答by Alternator

My problem of trying to bind to the GridViewColumn appears to be two-fold:

我试图绑定到 GridViewColumn 的问题似乎有两个方面:

  • Firstly the GridViewColumn can only provide one resultant value (probably the first item in the itemssource) as opposed to a value on a per row basis.

  • Secondly the GridViewColumn does not appear to exist in the visualtree and so is not available for binding to.

  • 首先,GridViewColumn 只能提供一个结果值(可能是 itemssource 中的第一项),而不是基于每行的值。

  • 其次 GridViewColumn 似乎不存在于可视化树中,因此不可用于绑定。

Further research shows that to bind to properties of the GridViewColumn, the following syntax can be used:

进一步的研究表明,要绑定到 GridViewColumn 的属性,可以使用以下语法:

<TextBlock Text="{Binding Path=View.Columns[0].Header, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" />

Obtained from MSDN: Passing a parameter into a CellTemplate specified in a GridViewColumn

MSDN 获取:将参数传递到 GridViewColumn 中指定的 CellTemplate