更改列表框列计入WPF后面的代码

时间:2014-04-15 09:42:19

标签: c# wpf listbox

如何在运行时更改列表框列数,这是我的代码

<ListBox HorizontalAlignment="Left" Margin="427,151,0,393" VerticalAlignment="Center" Height="480" Name="lst" ScrollViewer.HorizontalScrollBarVisibility="Auto" ItemTemplate="{StaticResource ListItemTemplate}" BorderThickness="0" Background="Transparent" SelectionChanged="lst_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Visible" VerticalContentAlignment="Center" HorizontalContentAlignment="Right">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="{Binding ColCount}"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

1 个答案:

答案 0 :(得分:3)

您只需在ColCount属性发生更改时通知。 一定要发送事件PropertyChanged(你的ViewModel必须从INotifyPropertyChanged继承)

public int  ColumnCount { get; private set; }

private void add()
    {
        ColumnCount += 1;
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("ColumnCount"));
    }

这对我来说很好!