开发者

XAML: UniformGrid where you can define cells as not unifrom in size?

开发者 https://www.devze.com 2023-02-15 08:36 出处:网络
As a part of an ItemsControl style I\'d like to have something that works similar ot Grid/UniformGrid as an ItemsPanel.

As a part of an ItemsControl style I'd like to have something that works similar ot Grid/UniformGrid as an ItemsPanel.

In its simplest form, the Style would look like this:

<Style TargetType="{x:Type ItemsControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <ItemsPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <UniformGrid Columns="2" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style>
                <Setter Property="Control.Margin" Value="10"/>
        开发者_JAVA百科    </Style>
        </Setter.Value>
    </Setter>
</Style>

and I would use it like this:

<WrapPanel Orientation="Vertical">
            <ItemsControl>
                <TextBlock Text="Label1:"/>
                <TextBlock Text="ThisWillBeBound" />
            </ItemsControl>
            <ItemsControl>
                <TextBlock Text="Label2:"/>
                <TextBlock Text="ThisWillBeBound" />
            </ItemsControl>
                            <ItemsControl>
                <TextBlock Text="Label3:"/>
                <TextBlock Text="ThisWillBeBound" />
            </ItemsControl>
            <ItemsControl>
                <TextBlock Text="Label4:"/>
                <TextBlock Text="ThisWillBeBoundButIsSomewhatLongerThenTheOtherProperties" />
            </ItemsControl>
            <ItemsControl>
                <TextBlock Text="Label5:"/>
                <TextBlock Text="ThisWillBeBound" />
            </ItemsControl>
            <ItemsControl>
                <TextBlock Text="Label6:"/>
                <TextBlock Text="ThisWillBeBound" />
            </ItemsControl>
        </WrapPanel>

As you can see, you would throw a Label for a property into this plus a binding to that property and they would stick together even if things would wrap around becuase of different sizes of the app window for example.

The nice thing here about theUniformGrid is that it seems that if one Label is longer the others will get the same space meaning the bound properties would line up neatly (why this happens I don't understand because these should be seperate UniformGrids I guess). Problem here is that the cell width of the Label part and the Property part is the same, so if the property is long there will be a large gap between the Label and the Property.

Using normal Grid instead would mean I'd have to set the Grid.Column="0" and Grid.Column="1" everyplace I put items into this ItemsControl. That's not really what I wan't.

Is there a way for me to have some sort of grid that lines up like the uniform one but doesn't stratch all columns when one only needs to be and assumes you are putting into columns the same way you declare the items?


The way I've typically seen this sort of scenario handled is via a combination of Grid+IsSharedSizeScope+ListView+DataTemplate. In this case, the label text (i.e. "Label1:") and entry text (i.e. "ThisWillBeBound") would be in a single item type and a collection of that type of item would be bound to the list view. The Grid.IsSharedSizeScope allows for sharing column size info between different entries while using a template - very handy. There's a great example of this at http://www.wpftutorial.net/DataTemplates.html. Is this the sort of scenario you're looking for?

0

精彩评论

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

关注公众号