在 DataTemplate 内绑定 Grid.Row/Grid.Column [英] Bind Grid.Row / Grid.Column inside a DataTemplate

查看:13
本文介绍了在 DataTemplate 内绑定 Grid.Row/Grid.Column的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这不是一个骗局.

我希望能够在 XAML 中执行以下操作:

I would like to be able to do the following in XAML:

<DataTemplate DataType="{x:Type TestApp:ButtonVM}">        
        <Button 
                Grid.Column="{Binding GridColumn}" 
                Grid.Row="{Binding GridRow}" 
                Content="{Binding Path=Info}" 
        />
</DataTemplate>

内容绑定工作正常,但生成的对象中根本不存在 Grid.Column 和 Grid.Row.甚至当我在没有绑定的情况下将它们设置为某个值时(如在 Grid.Column="1" 中).我窥探了该应用程序,发现在我的网格中没有人设置过 Grid.Column 和 Grid.Row.

The Content binding works fine but Grid.Column and Grid.Row simply don't exist in the produced object. Not even when I set them to some value without binding (like in Grid.Column="1"). I've snooped the application and saw that inside my grid nobody ever sets Grid.Column and Grid.Row.

有什么想法吗?

推荐答案

在博客的帮助下自己解决了.

Solved it myself with help from the blogs.

据我所知,您根本无法在内部进行附加属性绑定.

As far as I understand you simply can't do the attached property binding inside.

以下瞬间解决问题(ItemContainerStyle!):

The following solves the problem in an instant (ItemContainerStyle!):

<DataTemplate DataType="{x:Type TestApp:GridVM}">
        <ItemsControl ItemsSource="{Binding Path=Children}">
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Grid.Row" Value="{Binding GridRow}" />
                    <Setter Property="Grid.Column" Value="{Binding GridColumn}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid ShowGridLines="True"  Style="{Binding Path=Style}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height=".5*" />
                            <RowDefinition Height=".5*" />                            
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width=".5*" />
                            <ColumnDefinition Width=".5*" />
                        </Grid.ColumnDefinitions>                        
                    </Grid>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
</DataTemplate>

这篇关于在 DataTemplate 内绑定 Grid.Row/Grid.Column的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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