WPF列表框按钮来选择项目 [英] WPF ListBox Button Selected Item

查看:143
本文介绍了WPF列表框按钮来选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些的TextBlocks和一个按钮的列表框 - 按钮的codebehind它调用传递当前选定的列表框项的方法,这个伟大的工程。问题是,当我选择一个项目,然后点击其他项目不更新的SelectedItem属性的按钮 - 是有办法的XAML或C#,我可以强制点击一个按钮来选择父ListBoxItem中<? / P>

的XAML

 &LT;的DataTemplate&GT;
    &LT;电网&GT;
        &LT;按钮X:名称=myButton的点击=myButton_Click高度=30WIDTH =30&GT;
            &lt;图像源=资源\ Image.png/&GT;
        &LT; /按钮&GT;
        &LT; TextBlock的文本={结合数据字段}&GT;&LT; / TextBlock的&GT;
    &LT; /网格&GT;
&LT; / DataTemplate中&GT;
 

解决方案

  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;

这篇关于WPF列表框按钮来选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆