【问题标题】:How to handle a DataTemplate inside a tab?如何处理选项卡内的 DataTemplate?
【发布时间】:2014-12-13 01:34:18
【问题描述】:

我的名字是 Andrea,这是我的第一篇文章。

作为一个简单的读者,你经常帮助我,现在我写信是因为我想直接支持。

我必须创建一个选项卡控件,并使用“添加选项卡”按钮添加一个具有相同内容的新选项卡。 到目前为止一切都很好。

在选项卡中,我有一个文本编辑器和一个组合框。 我的问题有两个:

1 如何为我添加的每个选项卡加载组合框的内容? 2 每次我编写一个编辑选项卡覆盖的文本时,也会编辑另一个选项卡的文本。

代码如下:

Xaml 中的数据模板:

  <DataTemplate x:Key="tabItemContent">

            <dxlc:LayoutGroup Orientation="Vertical" Header="Target Description" IsCollapsible="True">
                <!--Name-->
                <dxlc:LayoutItem>
                    <dxlc:LayoutGroup Orientation="Horizontal" ItemSpace="4" >
                        <dxlc:LayoutItem Label="Name" Margin="10">
                            <dxe:TextEdit x:Name="TextEdit_NameTarget"/>
                        </dxlc:LayoutItem>
                    </dxlc:LayoutGroup>
                </dxlc:LayoutItem>
                <!--Nation e Label-->
                <dxlc:LayoutItem>
                    <dxlc:LayoutGroup Orientation="Horizontal" ItemSpace="12" >
                        <dxlc:LayoutItem Label="Nation"  Margin="10">
                            <ComboBox x:Name="ComboBox_TargetNazione" />
                        </dxlc:LayoutItem>                           
                    </dxlc:LayoutGroup>
                </dxlc:LayoutItem>                   
            </dxlc:LayoutGroup>
        </DataTemplate>

C#:

 private void Button_Click_Add(object sender, RoutedEventArgs e)
    {
        DataTemplate tabItemDataTemplate = this.TryFindResource("tabItemContent") as DataTemplate;
        DXTabItem tabItem = new DXTabItem();
        tabItem.Header = "New Tab";
        tabItem.ContentTemplate = tabItemDataTemplate;
        tabControl_Targets.Items.Add(tabItem);

    }

这里是将列表加载到组合框中的位置:

private void LoadComboBoxNation()
    {

        ComboBox_TargetNazione.ItemsSource =
          ManagementTriple.Istance().get_Nation_byTipologyAndContext(ComboBox_TypologyScenario.SelectedItem.ToString(),
          ComboBox_ContextScenario.SelectedItem.ToString());

        controlloselecteditem(ComboBox_SourceNazione.SelectedItem.ToString());

        controlloselecteditem(ComboBox_TargetNazione.SelectedItem.ToString());
    }

感谢大家给我的帮助。

【问题讨论】:

    标签: c# wpf visual-studio-2010 devexpress tabcontrol


    【解决方案1】:

    DataTemplate 需要一个简单但基本的要求才能正常工作:您应该使用ViewModel-First 方法。

    理想情况下,您的选项卡控件应该绑定到某个 ViewModel。然后,如果您希望显示另一个选项卡,您应该使用您的按钮单击在 ViewModel 中调用 Command,然后 ViewModel 会将另一个项目添加到您的 TabControl ItemsSource 属性(这将是一些集合) ,并且新项目将与其各自的 DataTemplate “自动”显示。

    WPF 的想法是用更间接的代码替换 View 中的所有这些命令式代码(就像您发布的那样),您唯一担心的是在 ViewModel 中操作事物,而“哑视图”紧随其后.

    希望这会有所帮助,但请随时在 cmets 中询问更多详细信息。

    【讨论】:

    • 好的,所以你的建议是改变策略?你有一个例子可以链接我吗?对这种方法不是很有经验。谢谢!!
    • 不幸的是,这不是一个简单的主题(我花了几个月的时间才完全理解 MVVM 架构)。但是,如果你用谷歌搜索“MVVM”、“DataTemplates”、“DataBinding”,最重要的是:“ViewModel-First”,你会得到一些很好的例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多