17

これがだまされないことを願っています。

XAML で次のことができるようにしたいと考えています。

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

Content バインディングは正常に機能しますが、生成されたオブジェクトには Grid.Column と Grid.Row が存在しません。バインドせずに値を設定した場合でも (Grid.Column="1" のように)。アプリケーションをスヌープしたところ、グリッド内で誰も Grid.Column と Grid.Row を設定していないことがわかりました。

何か案は?

4

1 に答える 1

27

ブログの助けを借りて自分で解決しました。

私が理解している限り、内部で添付プロパティのバインドを行うことはできません。

以下は瞬時に問題を解決します (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>
于 2010-03-12T13:36:28.823 に答える