[英]Binding selected item in listbox with datatemplate
I have simple listbox here: link to image where I have binded data. 我这里有简单的列表框: 链接到我有绑定数据的图像 。 Now when I tap on one item(one row) listbox item/row is selected/violet color but in fact this item is not real selected only row change color but when I click on image or text then row where I click in not selected/violin but item in code is selected.
现在,当我点击一个项目(一行)列表框项/行被选中/紫色但实际上这个项目不是真正选择只有行改变颜色但是当我点击图像或文本然后行我在哪里点击未选中/小提琴,但代码中的项目被选中。 I'm not sure if You understand what I'm saying.
我不确定你是否明白我在说什么。 In short if I click on blank space row then row is visual selected but sender like listbox item not get data, if I click on text or image then sender get data but its not visual selected.
简而言之,如果我点击空格行然后行是视觉选择但发件人像列表框项目不能获取数据,如果我点击文本或图像然后发送者获取数据但其不是视觉选择。 How I can do it when I click anywhere row is selected/violet and item-sender get data?
当我点击任何选择行/紫色和项目发送者获取数据时,我该怎么做?
And my second question is why my rectangle used like line has max width like text I want strech this rectangle at full width of listbox is this possible? 我的第二个问题是为什么我的矩形像线一样使用最大宽度像文本我想拉伸这个矩形在列表框的全宽度这可能吗?
XAML: XAML:
<ListBox x:Name="lbMessagesUsersList" Foreground="Black" ItemsSource="{Binding MyDatasMessagesUserList }">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Tapped="userTapped" Tag="{Binding}" >
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding MessengeHisPhoto}" HorizontalAlignment="Left" Width="40" Height="40" Margin="5,-18,0,-18" Stretch="Fill" ></Image>
<TextBlock x:Name="tbMessengerName" Text="{Binding MessengeFullName}" HorizontalAlignment="Left"
Grid.Column="1" Margin="25,0,0,0"/>
</Grid>
<Rectangle Height="1" Margin="0,0,0,-38" HorizontalAlignment="Stretch">
<Rectangle.Fill>
<SolidColorBrush Color="Black"/>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
CS: CS:
private void userTapped(object sender, TappedRoutedEventArgs e)
{
var button = sender as ListBoxItem;
if (button != null)
{
var subject = MyDatasMessagesUserList.FirstOrDefault(sub => sub == button.Tag);
if (subject != null)
{
IdOfChoosenUser = subject.MessengeIdUser;
}
}...
I also try remove ListbOxItem and set binding tag to stackPanel but this dont work too.{ 我也尝试删除ListbOxItem并将绑定标记设置为stackPanel,但这也不起作用。{
Note that, when you use an ItemTemplate
, each item gets wrapped in a ListBoxItem
-- so, you shouldn't use a ListBoxItem in the template, as that will produce two nested ones. 请注意,当您使用
ItemTemplate
,每个项目都包含在ListBoxItem
- 因此,您不应在模板中使用ListBoxItem,因为这将生成两个嵌套的项目。 Try removing it, like so: 尝试删除它,如下所示:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Tapped="userTapped" Tag="{Binding}">
<!-- content here -->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
For the handler, it might be easier to simply reference the ListBox by name instead of trying to use Tag
(thanks @MetroSmurf): 对于处理程序,可能更容易通过名称简单地引用ListBox而不是尝试使用
Tag
(感谢@MetroSmurf):
private void userTapped(object sender, TappedRoutedEventArgs e)
{
var selectedItem = lbMessagesUsersList.SelectedItem;
var subject = MyDatasMessagesUserList.FirstOrDefault(sub => sub == selectedItem);
}
For the second part of your question: to stretch the items, you need to stretch the ListBoxItem
wrapper. 对于问题的第二部分:要拉伸项目,需要拉伸
ListBoxItem
包装器。 Do this by using ItemContainerStyle
: 通过使用
ItemContainerStyle
执行此操作:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.