你好,
这里的需求似乎还有些不明确,假设我们有两个ItemControl,第一个在左,第二个在右,左侧其中一个Button点击以后你想要那个Content显示在右侧的哪里?另外既然你给右边的ItemControl指定了Source默认它会显示这个值的吧?
不过我想你可能想要知道怎么去绑定选择的Content,看以下的代码吧,应该对你有用:
XAML:
<Window x:Class="WPFItemControlTest1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFItemControlTest1"
Title="MainWindow" Height="379.289" Width="709.519">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Person}">
<Button Content="{Binding Name}" Tag="{Binding}" Click="Button_Click"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ItemsControl ItemsSource="{Binding}"/>
</StackPanel>
</Window>
CS:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFItemControlTest1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new List<Person>
{
new Person{Name = "One"},
new Person{Name = "Two"},
new Person{Name = "Three"},
};
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Person person = ((Button)sender).Tag as Person;
Trace.WriteLine(person.Name);
}
}
public class Person
{
public string Name { set; get; }
}
}
结果:

Barry
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.