TabItem Binding WPF

I am just learning WPF and I could use some help. I have an application that uses TabControl and dynamically generates new tabs, there is one TextBox on each tab, and now I would like to add a cancel button to the toolbar, which is not part of the tab (for example, VisualStudio). The cancel button should only work on the TextBox, which is located on the active tab, and if there is no tab or you can’t cancel, it will be disabled. And I have no idea how to link these two elements (the contents of the tab has its own xaml file).

The only thing I manage to do is add the clickHandler event to the MenuItem, and then find the text field on the active tab by name, but now I can not enable / disable as I would like.

I hope this is clear. Thanks for any help

+5
source share
3 answers

I gave an example to illustrate the “proper WPF way” to discuss this scenario. Again, this may not be the same as the code you already have, but it should give you some ideas on how to adapt your code. First, the code:

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; }
}

Nothing crazy here, I just create a model class and set up a collection of this class. Real kindness happens in 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>

Button ApplicationCommands.Undo, , TextBox. TabControl , , . . :

http://img706.imageshack.us/img706/2866/tabitembinding.png

, , undo , . , , .

+8

0

Writer WPF Application Framework (WAF). MDI ( Visual Studio) Undo/Redo. , .

0

All Articles