获取鼠标悬停时列表框项的内容以显示在不同的控件中

本文关键字:显示 控件 悬停 鼠标 列表 获取 | 更新日期: 2023-09-27 17:56:43

>我有一个ListBox,其中包含一个带有TextBlocks的数据绑定列表。现在,我希望此文本块显示在其他控件中。在本例中,它是一个文本框。

我设法更改以获取鼠标悬停事件并更改文本框的背景,但是获取ListBoxItem的内容似乎是不可能的?

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="myGroup"/>
            <ColumnDefinition/>
            <ColumnDefinition SharedSizeGroup="myGroup" Width="200"/>
        </Grid.ColumnDefinitions>
        <ListBox ItemsSource="{Binding}" Template="TextBlock" FontFamily="Courier New" Grid.Column="1" Name="lbox">
        </ListBox>
        <TextBox Grid.Column="2" x:Name="tbox">
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Setter Property="Text" Value="" />
                    <!-- Here is the 'normal' content -->
                    <Style.Triggers>
                        <!-- Here is how we bind to another control's property -->
                        <DataTrigger Binding="{Binding IsMouseOver, ElementName=lbox}" Value="True">
                            <Setter Property="Text" Value="AliceBlue" />
                            <!-- Here is the 'override' content -->
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
    </Grid>

获取鼠标悬停时列表框项的内容以显示在不同的控件中

如果我

正确理解您的问题,您应该执行以下操作

<Window x:Class="StkOverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:StkOverflow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" SharedSizeGroup="myGroup"/>
        <ColumnDefinition/>
        <ColumnDefinition SharedSizeGroup="myGroup" Width="200"/>
    </Grid.ColumnDefinitions>
    <ListBox ItemsSource="{Binding MyListItems}" FontFamily="Courier New" Grid.Column="1" Name="lbox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock  Text="{Binding Path=.}" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <TextBox Grid.Column="2" x:Name="tbox" Text="{Binding TextToShow}">
    </TextBox>
</Grid>

namespace StkOverflow
{
public partial class MainWindow
{
    public List<string> MyListItems
    {
        get { return (List<string>)GetValue(MyListItemsProperty); }
        set { SetValue(MyListItemsProperty, value); }
    }
    public static readonly DependencyProperty MyListItemsProperty =
        DependencyProperty.Register("MyListItems", typeof(List<string>), typeof(MainWindow), new PropertyMetadata(new List<string>() { "red", "orange", "green" }));

    public string TextToShow
    {
        get { return (string)GetValue(TextToShowProperty); }
        set { SetValue(TextToShowProperty, value); }
    }
    // Using a DependencyProperty as the backing store for TextToShow.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TextToShowProperty =
        DependencyProperty.Register("TextToShow", typeof(string), typeof(MainWindow), new PropertyMetadata(""));

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;           
    }
    private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
    {
        TextToShow = (sender as TextBlock).Text;
    }
    private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
    {
        TextToShow = "";
    }
}
}