简体   繁体   English

WP8-XAML Grid列在文本过长时扩展

[英]WP8 - XAML Grid column expanding when text is too long

I have a grid with column definitions. 我有一个带有列定义的网格。 Whenever the text inside a column cell exceeds the designated width, it shrinks the left hand column down. 每当列单元格中的文本超过指定的宽度时,它将缩小左侧的列。

Here's an example 这是一个例子

收缩柱状电池的示例

And this is my xaml markup 这是我的XAML标记

<ItemsControl Name="rfbItems" ItemsSource="{Binding}" 
            Style="{StaticResource contentItemsControl}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" Width="Auto" HorizontalAlignment="Left" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.3*" />
                    <ColumnDefinition Width="0.7*" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Title}" Style="{StaticResource App_Content_Grid}" TextAlignment="Left" />
                <TextBlock Grid.Column="1" Text="{Binding Description}" Style="{StaticResource App_Content_Grid_Subtle}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

How should I restrict this behavior without losing responsiveness in the layout? 我应该如何限制这种行为而又不失去版式的响应能力? Also, if there is a better way to achieve what I'm trying to do here (inside the datatemplate), please feel free to share :) 另外,如果有更好的方法(在datatemplate里面)实现我在这里想要做的事情,请随时分享:)

Try setting margin and horizontal alignment to text boxes as below: 尝试如下设置文本框的边距和水平对齐方式:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.3*" />
        <ColumnDefinition Width="0.7*" />
    </Grid.ColumnDefinitions>

    <TextBlock Grid.Column="0" Margin="5" HorizontalAlignment="Stretch" Text="{Binding Title}" Style="{StaticResource App_Content_Grid}" TextAlignment="Left" />
    <TextBlock Grid.Column="1" Margin="5" HorizontalAlignment="Stretch" Text="{Binding Description}" Style="{StaticResource App_Content_Grid_Subtle}" />

</Grid>

The Grid doesn't have a limited width unless you give your StackPanel a fixed width. 除非您为StackPanel提供固定的宽度,否则Grid的宽度没有限制。 Setting Width="123" should work, HorizontalAlignment="Stretch" might do it as well (untested). 设置Width =“ 123”应该可以,Horizo​​ntalAlignment =“ Stretch”也可以这样做(未经测试)。

I'm sorry for all your efforts, but I found a solution. 很抱歉您的所有努力,但我找到了解决方案。 None of the answers worked for me, so feel free to find a fitting solution and I'll accept your answer. 没有一个答案对我有用,所以随时找到合适的解决方案,我会接受您的回答。

<ItemsControl Name="rfbItems" ItemsSource="{Binding}" 
            Style="{StaticResource contentItemsControl}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" Width="Auto" HorizontalAlignment="Left" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="125" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding Title}" Style="{StaticResource App_Content_Grid}" TextAlignment="Left" />
                <TextBlock Grid.Column="1" Text="{Binding Description}" Style="{StaticResource App_Content_Grid_Subtle}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The first column has a fixed width, that's why the second column can't expand anymore. 第一列具有固定的宽度,这就是第二列无法再扩展的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM