gpt4 book ai didi

wpf - TabControl:SelectedContent 和 SelectedItem 返回相同

转载 作者:行者123 更新时间:2023-12-04 05:59:28 27 4
gpt4 key购买 nike

我正在动态显示自定义对象列表(此处:Customer),每个对象都使用 TabControl、ItemsSource 和 DataTemplate 在自己的选项卡中显示:

主Windows.xaml.cs:

using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace testTabControl
{
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int NumberOfContracts { get; set; }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

//create all
var customers = new List<Customer>
{
new Customer {FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23},
new Customer {FirstName = "Jane", LastName = "Smiths", NumberOfContracts = 42},
new Customer {FirstName = "John", LastName = "Tester", NumberOfContracts = 32}
};

//show
myTabControl.ItemsSource = customers;

}

private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
Trace.WriteLine("myTabControl.SelectedContent is " + myTabControl.SelectedContent.GetType());
Trace.WriteLine("myTabControl.SelectedItem is " + myTabControl.SelectedItem.GetType());

// do something with content of the selected tab:
///myTabControl.SelectedContent.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
}
}
}

主窗口.xaml:
    <Window x:Class="testTabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:testTabControl="clr-namespace:testTabControl"
Title="MainWindow" Height="350" Width="525"
SizeChanged="OnSizeChanged"
>
<Window.Resources>

<DataTemplate x:Key="myContent" DataType="{x:Type testTabControl:Customer}">
<TextBlock x:Name="myContentRoot">
This is the content for
<TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/>
</TextBlock>
</DataTemplate>

<DataTemplate x:Key="myHeader" DataType="{x:Type testTabControl:Customer}">
<TextBlock Text="{Binding LastName}"/>
</DataTemplate>

</Window.Resources>

<TabControl
x:Name="myTabControl"
SelectedIndex="0"
ItemTemplate="{StaticResource ResourceKey=myHeader}"
ContentTemplate="{StaticResource ResourceKey=myContent}"
/>

</Window>

在 OnSizeChanged 方法中,我希望 myTabControl.SelectedContent 返回从 DataTemplate 生成的 TextBlock。但是返回了一个 Customer 的实例!与 myTabControl.SelectedItem 相同。

我发现获取生成内容的唯一方法如下: http://msdn.microsoft.com/en-us/library/bb613579.aspx

有没有人知道没有可视化树行走的其他解决方案?

最佳答案

您的 TabControl包含类型 Customer 的对象. Templates可以用来告诉WPF如何绘制每个Customer对象,但是它不会改变 TabControl 中的项目的事实还在Customer对象。

如果您想访问该项目的模板化 UI 对象,您可以走 VisualTree或使用 ItemContainerGenerator获取装有 SelectedItem 的容器

例如,

var selectedItem = myTabControl.SelectedItem;

var selectedItemContainer =
myTabControl.ItemContainerGenerator.ContainerFromItem(selectedItem);

关于wpf - TabControl:SelectedContent 和 SelectedItem 返回相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9098197/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com