我有一个ListBox,其中包含带有TextBlocks的数据绑定列表。现在我希望这个文本块以不同的控件显示。在这种情况下,它是一个TextBox。
我设法改变以获取鼠标悬停事件并更改文本框的背景,但获取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>
答案 0 :(得分: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 = "";
}
}
}