我只是在学习WPF,我需要一些帮助。我有一个使用TabControl并动态生成新选项卡的应用程序,在每个选项卡上我都有一个TextBox,现在我想在工具栏上添加一个撤销按钮,这个工具栏不是选项卡的一部分(VisualStudio类似)。撤消按钮只能在活动选项卡上的TextBox上工作,如果没有选项卡或无法执行撤消,则将禁用该选项卡。我不知道如何绑定这两个项(选项卡内容拥有自己的xaml文件)。
唯一成功的方法是向eventHandler中添加一个单击MenuItem,然后按名称在活动选项卡上找到文本框,但现在我无法像我所希望的那样启用/禁用。
希望这是可以理解的。谢谢你的帮助
发布于 2010-06-03 16:33:37
我举了一个例子来说明“正确的WPF方式”来实现这个场景。同样,它可能与您已经拥有的代码不匹配,但是它应该给您一些关于如何调整代码的想法。首先,代码隐藏:
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中:
<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命令将自动禁用自己。因此,如果没有选项卡页,它将被禁用,而不需要任何额外的代码。
发布于 2010-06-03 16:07:39
您想要的是使用更容易地完成--基本上,它有一个内置的"CanExecute“事件,您可以在该事件中检查TabControl的选定页面等等。如果没有使用任何示例代码,很难给出一个具体的示例,但希望这将使您走上正确的道路。
发布于 2010-06-03 22:17:00
您可能感兴趣的是Writer示例应用程序的WPF应用框架(WAF)项目。它展示了一个带有选项卡式MDI (类似于Visual )的简单编辑器,并实现了撤销/重做函数。这可能正是你要找的。
https://stackoverflow.com/questions/2967581
复制相似问题