[WPF] UniformGrid 元素能不能通过C#代码调用?该如何解决

[WPF] UniformGrid 元素能不能通过C#代码调用?GridListBox Height110 HorizontalAlignmentLeft M

[WPF] UniformGrid 元素能不能通过C#代码调用?

<Grid>
        <ListBox Height="110" HorizontalAlignment="Left" Margin="282,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="193" SelectionMode="Extended">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Name="ufgrid1"></UniformGrid>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
</Grid>

有没有办法通过后台代码调用UniformGrid 元素,可以更改Columns和Rows?

UniformGrid是为了让ListBox显示多行多列。




[解决办法]
使用绑定:
<ItemsPanelTemplate>
            <UniformGrid IsItemsHost="True" Columns="{Binding Path=MyColumnCount}"
                         VerticalAlignment="Top" />
        </ItemsPanelTemplate>