2016-07-12 5 views
0

私はTextBlocksでデータバインドされたリストを含むListBoxを持っています。今度は、このテキストブロックを別のコントロールに表示します。この場合、それはTextBoxです。MouseOverでListBoxItemの内容を別のコントロールに表示する

私はmouseoverイベントを取得してテキストボックスの背景を変更することができましたが、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> 

答えて

1

私が正しくあなたの問題を理解していれば、あなたはこの

<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 = ""; 
    } 
} 
} 
ような何かを行う必要があります
関連する問題