TabItem的WPF绑定 [英] TabItem Binding WPF

查看:1726
本文介绍了TabItem的WPF绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是学习WPF和我可以利用一些帮助。
我有使用TabControl的一个应用程序,并动态生成新的选项卡,在每个选项卡我有一个文本框,现在我想撤消按钮添加到工具栏这不是选项卡(类似​​的VisualStudio)的一部分。撤消按钮有只在文本框至极的工作是活动标签,如果没有标签或无法执行撤消其将被禁用。而且我不知道如何绑定这两个项目(标签内容有它自己的XAML文件)。

I'm just learning WPF and I could use some help. I have an application which use TabControl and dynamically generates new tabs,on each Tab I have one TextBox and now I'd like to add an undo button to the tool bar which is not the part of the tab (VisualStudio like). The undo button has to work only on the TextBox wich is on the active tab and if there is no tab or the undo can't be executed it will be disabled. And I have no idea how to bind these two items (The tab content has it own xaml file).

我只被成功添加一个点击事件处理程序的菜单项,然后用域名来激活选项卡上的文本框,但现在我不能启用/禁用像我想的事情。

Only thing I succeed is adding an click eventHandler to the MenuItem and then finding the text box on the active tab by name, but now I can't enable/disable like I'd wish.

我希望这是可以理解的。
感谢您的帮助。

I hope it is understandable. Thanks for any help

推荐答案

我做了一个例子来说明的正确的方式WPF的去了解这一情况。再次,它可能不匹配code你已经有了,但它应该给你如何去适应你的code的一些想法。首先,code-背后:

I've made an example to illustrate the "right WPF way" to go about this scenario. Again, it might not match the code you already have, but it should give you some ideas on how to adapt your code. First, the code-behind:

public partial class TabItemBinding : Window
{
    public ObservableCollection<TextItem> Items { get; set; }

    public TabItemBinding()
    {
        Items = new ObservableCollection<TextItem>();

        Items.Add(new TextItem() { Header = "1", Content = new TextBox() { Text = "First item" } });
        Items.Add(new TextItem() { Header = "2", Content = new TextBox() { Text = "Second item" } });
        Items.Add(new TextItem() { Header = "3", Content = new TextBox() { Text = "Third item" } });

        InitializeComponent();
    }
}

public class TextItem
{
    public string Header { get; set; }
    public FrameworkElement Content { get; set; }
}

没有什么疯狂的在这里,我只是创建模型类,并建立了类的集合。真正的善良发生在XAML:

Nothing crazy here, I'm just creating a model class and setting up a collection of that class. The real goodness happens in the XAML:

<Window x:Class="TestWpfApplication.TabItemBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TabItemBinding" Height="300" Width="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <ToolBar Grid.Row="0">
        <Button Command="Undo">Undo</Button>
    </ToolBar>

    <TabControl Grid.Row="1" ItemsSource="{Binding Items}">
        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding Header}"/>
                <Setter Property="Content" Value="{Binding Content}"/>
            </Style>
        </TabControl.ItemContainerStyle>
    </TabControl>
</Grid>

我勾了按钮 ApplicationCommands.Undo ,它会自动照顾撤消对我们来说只要我们有一个活动的编辑文本框。在的TabControl 本身必然要我们在$ C $国产C后面的集合,这将提供一个标题和一些文本进行编辑。而我们所做的任何修改将是可以撤销的。其结果是:

I hook up the Button to ApplicationCommands.Undo, which will automatically take care of the undo for us as long as we have an active editing TextBox. The TabControl itself is bound to the collection we made in the code-behind, which will provide a header and some text to edit. And any edits we make will be undo-able. The result:

另外,需要注意的是,如果没有积极的编辑上下文撤消命令将自动禁用本身是很重要的。所以,如果没有标签页,将不会对我们的一部分,任何额外的code禁用。

By the way, it's important to note that the undo command will automatically disable itself if there is not an active editing context. So if there are no tab pages, it will be disabled without any extra code on our part.

这篇关于TabItem的WPF绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆