WPF列表框按钮来选择项目按钮、项目、列表、WPF

2023-09-05 02:44:03 作者:睫毛上の泪

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

的XAML

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

解决方案

  VAR curItem =((ListBoxItem的)myListBox.ContainerFromElement((按钮)发送方))的内容;
 

C WPF界面设计,不同按钮显示不同的界面,需要新建多个页面吗 如何按各自的按钮显示各自的页面

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;