DataTemplate中grid的宽度问题

wxcnl 2014-03-11 10:38:15
看下面代码,listbox引用的数据模板,并且邦定了一个集合数据,现在想实现点击每一个item的内容触发command,问题是listbox item的宽度为自动伸展的,也就是100%了,但是grid的宽度却是随着文本的宽度的变化而变化,也就是说grid的宽度和文本的宽度一样,后果是点击文本内容才能触发command,但我现在需要点击整个item的内容也要触发command,也就是说如何让grid或者stackpanel的宽度和listbox item的宽度一样长?或者说让他们伸展到100%?

<DataTemplate x:Key="RecentProjectTemplate">
<Grid Margin="0,10,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions> <StackPanel Grid.Column="1" Grid.Row="0" Background="Red" Margin="0,0,0,0">
<StackPanel.InputBindings>
<MouseBinding Command="{Binding DataContext.RecentProjectsCommand, RelativeSource={RelativeSource AncestorType=ListBox}}" MouseAction="LeftClick" />
</StackPanel.InputBindings>
<TextBlock Foreground="Black" Text="{Binding}" Margin="20,0,0,0">
</TextBlock>
</StackPanel>
</Grid>
</DataTemplate>


<ListBox BorderThickness="0" Margin="0,0,0,0" ItemTemplate="{StaticResource RecentProjectTemplate}" ItemsSource="{Binding RecentProjects}">
...全文
434 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Capdiem 2016-04-07
  • 打赏
  • 举报
回复
<ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" />
wxcnl 2014-03-11
  • 打赏
  • 举报
回复
不行啊!之前都尝试过了
Bonjour-你好 2014-03-11
  • 打赏
  • 举报
回复
ListBox中添加 HorizontalContentAlignment="Stretch" 试试
小恒丶 2014-03-11
  • 打赏
  • 举报
回复
模板里面修改你的大小啊
wxcnl 2014-03-11
  • 打赏
  • 举报
回复
达人啊,求解~~~

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧