开发者

Layout and formatting without Dynamic Controls in WPF

How do I format and throw my controls in a layout in WPF? I mean, I know about making dynamic controls and throwing them on a Grid or a Panel or something, but is there a better way? Like if this were a web app, I could just use some HTML and CSS and get what I want but is there something better than creating dynamic controls in WPF? I have database table full of stuff which I want displayed 开发者_开发问答in my WPF application. Can I modify the DataGrid or ListView or something else for this purpose? I'm attaching images, which might make it easier for you guys to understand what I'm talking about.

images:

my data, inside the database table - http://i.stack.imgur.com/LnHCv.png

my data, formatted with HTML and CSS - http://i.stack.imgur.com/6PHBH.png


The typical way to do this in WPF is via Data Templating.

In general, if you want to display a set of data formatted as per your second screenshot, you'd bind the dataset (usually contained in a collection such as ObservableCollection) to a ListBox or similar child of ItemsControl, and set the ItemsTemplate to a DataTemplate you've created to display the items in the format you desire.

For example, a very rough approximation of your HTML layout:

<ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="TypeOfYourDataObject">
            <StackPanel>
                <TextBlock FontSize="12" Foreground="Turquoise" Text="{Binding NickName}"/>
                <StackPanel Orientation="Horizontal">
                    <TextBlock FontSize="11" Foreground="Gray" Text="email / "/>
                    <TextBlock FontSize="11" Foreground="Turquoise" Text="{Binding EmailAddress}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock FontSize="11" Foreground="Gray" Text="web / "/>
                    <TextBlock FontSize="11" Foreground="Turquoise" Text="{Binding Url}"/>
                </StackPanel>
                <Separator/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜