我有一些的TextBlocks和一个按钮的列表框 - 按钮的codebehind它调用传递当前选定的列表框项的方法,这个伟大的工程。问题是,当我选择一个项目,然后点击其他项目不更新的SelectedItem属性的按钮 - 是有办法的XAML或C#,我可以强制点击一个按钮来选择父ListBoxItem中
的XAML
<的DataTemplate>
<电网>
<按钮X:名称=myButton的点击=myButton_Click高度=30WIDTH =30>
<图像源=资源\ Image.png/>
< /按钮>
< TextBlock的文本={结合数据字段}>< / TextBlock的>
< /网格>
< / DataTemplate中>
解决方案
VAR curItem =((ListBoxItem的)myListBox.ContainerFromElement((按钮)发送方))的内容;
I have a listbox with some textblocks and a button -- in the button's codebehind it calls a method passing the currently selected listbox item, this works great. The issue is that when I select an item and then click the button on another item it doesn't update the "SelectedItem" property -- is there a way Xaml or C# that I can force a button click to select the parent ListBoxItem?
Xaml
<DataTemplate>
<Grid>
<Button x:Name="myButton" Click="myButton_Click" Height="30" Width="30">
<Image Source="Resources\Image.png" />
</Button>
<TextBlock Text="{Binding DataField}"></TextBlock>
</Grid>
</DataTemplate>
解决方案
var curItem = ((ListBoxItem)myListBox.ContainerFromElement((Button)sender)).Content;