【问题标题】:How to get textblock name for the selected TabItem from TabControl如何从 TabControl 获取所选 TabItem 的文本块名称
【发布时间】:2020-01-20 09:15:46
【问题描述】:

我为 TabControl 动态创建了 5 种类型的 TabItem。例如:

 <Style x:Key="StyleCrcs" TargetType="{x:Type TabItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="AllowDrop" Value="True"/>
        <EventSetter Event="PreviewMouseMove" Handler="TabItem_PreviewMouseMove"/>
        <EventSetter Event="Drop" Handler="TabItem_Drop"/>
        <Setter Property="ContentTemplate" >
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        <TextBlock TextWrapping="Wrap" Text="Adres(hex)" FontSize="15" Margin="10,10,396,444"/>
                        <ComboBox x:Name="AddressCrcs" HorizontalAlignment="Left" Margin="134,11,0,0" VerticalAlignment="Top" Width="120">
                            <ComboBoxItem>60</ComboBoxItem>
                            <ComboBoxItem>61</ComboBoxItem>
                            <ComboBoxItem>62</ComboBoxItem>
                            <ComboBoxItem>63</ComboBoxItem>
                            <ComboBoxItem>64</ComboBoxItem>
                            <ComboBoxItem>65</ComboBoxItem>
                            <ComboBoxItem>66</ComboBoxItem>
                            <ComboBoxItem>67</ComboBoxItem>
                        </ComboBox>

                        <TextBlock x:Name="Tranz1" TextWrapping="Wrap" Text="Tranzystor 1:" FontSize="15" Margin="10,126,396,337"/>
                        <TextBlock x:Name="Tranz2" TextWrapping="Wrap" Text="Tranzystor 2:" FontSize="15" Margin="10,156,396,307"/>
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

每个都有特定的结构。 所有这些类型都从 ICards 继承。

interface ICards
{
    string Name { get; set; }
    string Address { get; set; }
}
class CardCrcs : ICards
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string Tranzystor1 { get; set; }
    public string Tranzystor2 { get; set; }
}

当我创建 TabItem 时,我将对象添加到列表中。

每个 TabItem 都有文本块,当我更改 TabItem 时,我想为此文本块设置文本。

我该怎么做?

<TabControl x:Name="TabCards" Grid.Column="1" SelectionChanged="TabCards_SelectionChanged">

var list = new List<ICards>()
var element = (CardCrcs)list[0];
TabCards.SelectedItem.Tranz1.Text = element.Tranzystor1;

TabCards.SelectedItem.Tranz1.Text不工作。如何替换这个来做到这一点?

编辑:

当我这样做时,我可以获取标题,但不能获取文本块名称。

var item = (TabItem)TabCards.SelectedItem;   

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我建议将您的 TabControl 绑定到您还为其提供 DataTemplates 的视图模型实例列表(将映射为选项卡项的 ContentTemplate)。

    参考this question and its answer

    您似乎已经定义了不同的视图模型(例如 CardCrcs 类),因此您可以定义 DataTemplate 对象,并为每个对象设置 TargetType,描述每个选项卡项的外观。

    在每个模板中,您可以使用绑定到由相应类定义的属性,例如&lt;TextBlock Text="{Binding Address}"/&gt;.

    最后,将 TabControl 的 ItemsSource 设置为视图模型实例列表,如果您愿意,可以在 DataContext 中准备这些实例:ItemsSource={Binding}

    这种方法会限制很多背后的代码,而且也更容易理解,IMO。

    【讨论】:

    • 我不确定我是否理解你。我这样做的原因是。用户选择添加卡片、添加卡片的数量和顺序。所以我认为定义整个tabitem是可以的。感谢您的回复和建议。
    • 对不起,也许我太具体了。这里有一些 documentationguide 用于理解 WPF 中的绑定和 MVVM 方法(我的回答中使用了这些概念)。您不需要那里的所有信息,但请尝试获取概览。后一个链接指向较旧的资源,但概念仍然适用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2023-03-14
    • 2011-02-14
    相关资源
    最近更新 更多