更改模板后保存控件的内容

时间:2017-03-04 07:55:16

标签: .net wpf

我在DataTemplate中有两个Window.ResourcesClick事件按钮的处理程序会更改ContentPresenter.ContentTemplate的{​​{1}}。



Grid




<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="Template1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>                    
                </Grid.ColumnDefinitions>
                <TextBox/>
                <TextBox Grid.Row="1"/>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="Template2">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBox/>
                <TextBox Grid.Row="1"/>
                <TextBox Grid.Row="2"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Button x:Name="Template1Button" Content="Template1" Click="Template1Button_Click"/>
            <Button x:Name="Template2Button" Grid.Column="1" Content="Template2" Click="Template2Button_Click"/>
        </Grid>
        <Grid Grid.Row="1">
            <ContentPresenter x:Name="contentPresenter" ContentTemplate="{DynamicResource Template1}"/>
        </Grid>
    </Grid>
</Window>

更改TextBoxes的模板内容后,不保存。是否可以保存此数据,例如更改呈现模板的namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Template1Button_Click(object sender, RoutedEventArgs e) { contentPresenter.ContentTemplate = (DataTemplate)Resources["Template1"]; } private void Template2Button_Click(object sender, RoutedEventArgs e) { contentPresenter.ContentTemplate = (DataTemplate)Resources["Template2"]; } } } 元素?

1 个答案:

答案 0 :(得分:1)

可以在DataContext中保存数据。使用字符串属性创建一个简单的类:

public class Str
{
    public string S1 { get; set; }
    public string S2 { get; set; }
    public string S3 { get; set; }
}

将实例分配给Window.DataContext

InitializeComponent();
DataContext = new Str(); 

并修改模板以使用绑定:

<DataTemplate x:Key="Template1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBox Text="{Binding Path=S1}"/>
        <TextBox Grid.Row="1" Text="{Binding Path=S2}"/>
    </Grid>
</DataTemplate>
<DataTemplate x:Key="Template2">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBox Text="{Binding Path=S1}"/>
        <TextBox Grid.Row="1" Text="{Binding Path=S2}"/>
        <TextBox Grid.Row="2" Text="{Binding Path=S3}"/>
    </Grid>
</DataTemplate>

对contentPresenter进行微小更改

<ContentPresenter x:Name="contentPresenter" 
                  Content="{Binding}" ContentTemplate="{DynamicResource Template1}"/>