Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在DataTemplate内绑定Grid.Row/Grid.Column_Wpf_Data Binding_Mvvm_Attached Properties - Fatal编程技术网

Wpf 在DataTemplate内绑定Grid.Row/Grid.Column

Wpf 在DataTemplate内绑定Grid.Row/Grid.Column,wpf,data-binding,mvvm,attached-properties,Wpf,Data Binding,Mvvm,Attached Properties,希望这不是一个骗局 我希望能够在XAML中执行以下操作: <DataTemplate DataType="{x:Type TestApp:ButtonVM}"> <Button Grid.Column="{Binding GridColumn}" Grid.Row="{Binding GridRow}" Content="{Binding Pat

希望这不是一个骗局

我希望能够在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


有什么想法吗?

在博客的帮助下,我自己解决了这个问题

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

以下内容立即解决了问题(ItemContainerStyle!):



如何将ButtonVM对象放入网格?网格不是项控件,因此它不会将任意视图模型对象作为其子对象。秘密是使用ItemsControl.itemsContainerStyle并在那里使用setter将绑定注入到模板化的子项中。啊,是的。应该这样做。附加的网格。行需要是网格的直接子对象。我们在这方面遇到了很多问题,并尝试了许多其他解决方案,这是唯一有效的解决方案!!!
<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>