1

にコントロールTabControl付きがあります。あるタブにテキストを入力して別のタブに切り替えると、元のタブに戻ったときに元のタブの元に戻す履歴が消えています。TextBoxContentTemplate

発生する別の問題は、選択されたテキストの選択が解除され、キャレットが の先頭に移動することTextBoxです。

ハードコーディングされTabItemたコントロールだけでウィンドウを作成すると、元に戻す履歴が保持されます。この問題は、バインディングまたはテンプレートに関係しています。

メイン ウィンドウの XAML は次のとおりです。

<Window x:Class="TabbedTextAreaTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Command="{Binding AddNewTab}">Add Tab</Button>
        <TabControl ItemsSource="{Binding Tabs}" Grid.Row="1">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}"/>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
</Window>

これらのコマンドを手動でキャッチせずにタブを切り替えるときに、元に戻す/やり直しの履歴と選択したテキストを保存する方法はありますか?

4

2 に答える 2

2

When you use a TabControl which gets its tabs via databinding on ItemsSource, WPF doesn't keep the visual tree for each item around as you switch. Thus, when you switch from tab 1 to tab 2, then back to tab 1, the controls on tab 1 are not actually the same control instances which you saw on tab 1 the first time.

There are a number of ways around to deal with this - TabControls which have explicit TabItem instances do keep their visual trees when you switch tabs, so probably the easiest way to do it is to wrap your collection of tab items in something which makes TabItems for them.

Unfortunately right now I can't find a link to an example of how to do this. There are references to articles elsewhere on SO, but they all seem to point to pages which no longer exist, and I don't have time to dig any deeper.

于 2012-06-14T15:37:14.887 に答える
1

理由は簡単です。不平を言う両方の操作が厳密にUI操作であると思われる場合:元に戻す:UIコントロールでのユーザー編集選択:UIコントロールでのテキストの選択

別のコントロールに切り替えTabて戻ると、WPFで発生するのは、すべてのコントロールが、初めて表示した場合と同じように、データに(通常または単に)再バインドされることです。そのため、UIの外観属性が失われます。ModelViewModel

Tabの環境でそれを正しく管理するには、スタックを自分WPFで管理する必要があります。Undo/Redo

幸運を。

于 2012-06-14T15:33:16.107 に答える