开发者

WPF - repeat elements from list

开发者 https://www.devze.com 2023-03-03 02:41 出处:网络
I try to do a WPF Application. The Application should look like the followings. Title - top left Button \"New Entry\" - top right

I try to do a WPF Application. The Application should look like the followings.

Title - top left Button "New Entry" - top right after this, there is a table or something like this. with three columns per entry. On the first column the name, second the text and on the third column a button.

The name, text and the button should be shown and repeated for every element in my generic list.

How can I do this? Is there a repeater control like in WebFo开发者_如何学JAVArms? Can I use the ListView? If so, how can I configure it?

Any help would be appreciated.

thanks!


What you should do is create a dataGrid in xaml, bind the columns to your fields, and then set the itemssource to that data grid:

<DataGrid 
    x:Name="ItemsDataGrid"
    AutoGenerateColumns="False"
    HeadersVisibility="Column"
    ItemsSource="{Binding Path=YourDataCollection, Mode=OneWay}"
    SelectionChanged="IfYouWantToDoSomethingHereHandler"
    >
    <DataGrid.Columns>
        <DataGridTextColumn 
            Header="Name"
            Binding="{Binding Path=NameFromYourDataObject, Mode=TwoWay}" 
            Width="Auto"
            />
        <DataGridTextColumn 
            Header="DescriptionText"
            Binding="{Binding Path=DescriptionFromYourDataObject, Mode=TwoWay}" 
            Width="Auto"
            />
        <DataGridTemplateColumn 
            Header="ButtonColumn"
            CellTemplate="{StaticResource ButtonTemplate}"
            Width="Auto"
             />
    </DataGrid.Columns>
</DataGrid>

Where ButtonTemplate is a DataTemplate:

<DataTemplate
    x:Key="ButtonTemplate"
    >
        <Button
            -- bind a command here of what the pressing of the button should look like
            />
</DataTemplate>

All you have to do from here on is create the DataObject, create a List or ObservableCollection("YourDataCollection"), fill it up with this data, and then set the ItemsSource of ItemsDataGrid.

That should be it.. HTH, Daniel

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号