WPF ListViewItem 宽度绑定未按预期工作
我在将 listviewitems 的大小设置为网格第二列中可用的空间时遇到问题。我有一个 ValidationItemError 类,它可以有很长的描述。这是它的 DataTemplate xaml。
<DataTemplate DataType="{x:Type Models:ValidationItemError}">
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Ellipse Width="10"
Height="10"
Margin="5,0,0,0"
Fill="Crimson" />
<TextBlock Text="{Binding ColumnName}"
Grid.Column="1"
FontWeight="Bold" />
</Grid>
<TextBlock Text="{Binding Description}"
ToolTip="{Binding Description}"
Margin="20,0,0,0"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
validationItemErrors 被分组到另一个类 ValidationLineItem 中,这是它的 DataTemplate
<DataTemplate DataType="{x:Type Models:ValidationLineItem}">
<StackPanel>
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="Line"
FontSize="16"
FontWeight="Bold" />
<TextBlock Text="{Binding LineNumber}"
Margin="5,0,0,0"
FontSize="16"
Grid.Column="1" />
</Grid>
<ItemsControl ItemsSource="{Binding ValidationItems}">
</ItemsControl>
</StackPanel>
</DataTemplate>
所以基本上我有一个validationLineItem,它存储行号和存储在 ValidationItems 属性中的多个 ValidationItemError/Warnings。所以现在我只需将该数据绑定到网格左列上的列表视图。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75*" />
<ColumnDefinition Name="GridCell" Width="25*" />
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="1">
<ListView ItemsSource="{Binding ValidationItems}"
SelectedItem="{Binding SelectedValidationItem}"
SelectionChanged="ListViewSelectionChanged">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Width"
Value="{Binding ElementName=GridCell, Path=Width}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</ScrollViewer>
</Grid>
除了这部分之外,所有内容都渲染得很好
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Width"
Value="{Binding ElementName=GridCell, Path=Width}" />
</Style>
</ListView.ItemContainerStyle>
。描述文本块会忽略宽度设置,并且不会像我需要的那样换行。 如果我手动设置宽度,
<Setter Property="Width" Value="100" />
而不是像
<Setter Property="Width" Value="{Binding ElementName=GridCell, Path=Width}" />
我想要的那样,描述文本块会换行。 有人有什么想法吗?我确信我在这里遗漏了一些简单的东西。
我应该指出,我也已经尝试过这个但没有运气。
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
I am having a problem with getting the listviewitems to size to the space available to it in the second column of a grid. I have a ValidationItemError class that can have a long description. This is the DataTemplate xaml for it.
<DataTemplate DataType="{x:Type Models:ValidationItemError}">
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Ellipse Width="10"
Height="10"
Margin="5,0,0,0"
Fill="Crimson" />
<TextBlock Text="{Binding ColumnName}"
Grid.Column="1"
FontWeight="Bold" />
</Grid>
<TextBlock Text="{Binding Description}"
ToolTip="{Binding Description}"
Margin="20,0,0,0"
TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
The validationItemErrors are grouped into another class ValidationLineItem and this is its DataTemplate
<DataTemplate DataType="{x:Type Models:ValidationLineItem}">
<StackPanel>
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="Line"
FontSize="16"
FontWeight="Bold" />
<TextBlock Text="{Binding LineNumber}"
Margin="5,0,0,0"
FontSize="16"
Grid.Column="1" />
</Grid>
<ItemsControl ItemsSource="{Binding ValidationItems}">
</ItemsControl>
</StackPanel>
</DataTemplate>
So basically I have a validationLineItem which stores the linenumber and multiple ValidationItemError/Warnings stored in the ValidationItems property. So now I just bind that data to a listview that is on left column of the grid.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75*" />
<ColumnDefinition Name="GridCell" Width="25*" />
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="1">
<ListView ItemsSource="{Binding ValidationItems}"
SelectedItem="{Binding SelectedValidationItem}"
SelectionChanged="ListViewSelectionChanged">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Width"
Value="{Binding ElementName=GridCell, Path=Width}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</ScrollViewer>
</Grid>
That all renders fine except for this part
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Width"
Value="{Binding ElementName=GridCell, Path=Width}" />
</Style>
</ListView.ItemContainerStyle>
The description textblock ignores the width setting and does not wrap like I need it to.
If I manually set the width like
<Setter Property="Width" Value="100" />
instead of
<Setter Property="Width" Value="{Binding ElementName=GridCell, Path=Width}" />
The description textblock wraps as I would like.
Anyone have any ideas? I am sure I'm missing something simple here.
I should note, that I have already tried this too with no luck.
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是
ListView
有一个内部内置ScrollViewer
导致问题。在我看来,您只是想让内容换行。因此,放弃
ItemContainerStyle
并将其添加到ListView
中:这将禁用水平滚动条并使您的内容换行!
希望这有帮助!
为了完整性:
Your problem is that the
ListView
has an internal buildinScrollViewer
that causes the problem.It seems to me, that you just want the content to wrap. So ditch the
ItemContainerStyle
and add this to theListView
:This will disable the horizontal scrollbar and makes your content wrap!
Hope this helps!
For completeness: