Wpf 相同的XAML“模板”,不同的绑定-如何操作

Wpf 相同的XAML“模板”,不同的绑定-如何操作,wpf,xaml,Wpf,Xaml,我认为我在标题中的措辞不太好,所以让我解释得更好一点。首先,这是一个使用MVVM和C的WPF项目,问题与XAML有关 我所拥有的并不是严格意义上的“模板”,但重复意味着肯定有一种方法可以将其浓缩 让我从感兴趣的XAML开始,它并不像看上去那么复杂 <Expander ExpandDirection="Down" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Margin="2" Header="{Binding VerbFormsView

我认为我在标题中的措辞不太好,所以让我解释得更好一点。首先,这是一个使用MVVM和C的WPF项目,问题与XAML有关

我所拥有的并不是严格意义上的“模板”,但重复意味着肯定有一种方法可以将其浓缩

让我从感兴趣的XAML开始,它并不像看上去那么复杂

<Expander ExpandDirection="Down" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Margin="2" Header="{Binding VerbFormsViewModel_PresentIndicative.Name}">
    <Border BorderBrush="Black" CornerRadius="5,5,5,5" Margin="2" BorderThickness="2">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <StackPanel Margin="2">
                    <TextBlock Margin="3" Text="{Binding VerbFormsViewModel_PresentIndicative.PlainPositive_Name}" TextAlignment="Center" FontSize="12"
                                           MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"/>
                    <TextBlock Margin="2">English translation:</TextBlock>
                    <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                                         Text="{Binding VerbFormsViewModel_PresentIndicative.PlainPositive_TranslationEnglish, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    <TextBlock Margin="2">Japanese kanji:</TextBlock>
                    <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                                         Text="{Binding VerbFormsViewModel_PresentIndicative.PlainPositive_TranslationJapaneseKanji, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    <TextBlock Margin="2">Japanese hiragana:</TextBlock>
                    <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                                         Text="{Binding VerbFormsViewModel_PresentIndicative.PlainPositive_TranslationJapaneseHiragana, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    <TextBlock Margin="2">Japanese romaji:</TextBlock>
                    <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                                         Text="{Binding VerbFormsViewModel_PresentIndicative.PlainPositive_TranslationJapaneseRomaji, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                </StackPanel>

                <StackPanel Margin="2">
                    <TextBlock Margin="3" Text="{Binding VerbFormsViewModel_PresentIndicative.PlainNegative_Name}" TextAlignment="Center" FontSize="12"
                                           MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"/>
                    <TextBlock Margin="2">English translation:</TextBlock>
                    <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                                         Text="{Binding VerbFormsViewModel_PresentIndicative.PlainNegative_TranslationEnglish, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    <TextBlock Margin="2">Japanese kanji:</TextBlock>
                    <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                                         Text="{Binding VerbFormsViewModel_PresentIndicative.PlainNegative_TranslationJapaneseKanji, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    <TextBlock Margin="2">Japanese hiragana:</TextBlock>
                    <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                                         Text="{Binding VerbFormsViewModel_PresentIndicative.PlainNegative_TranslationJapaneseHiragana, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                    <TextBlock Margin="2">Japanese romaji:</TextBlock>
                    <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                                         Text="{Binding VerbFormsViewModel_PresentIndicative.PlainNegative_TranslationJapaneseRomaji, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                </StackPanel>

            </StackPanel>
        </StackPanel>
    </Border>
</Expander>
现在,正如您所看到的,上面的全部内容都依赖于对某个名为VerbFormsViewModel_PresentIndicative的对象的绑定。这代表的是动词的形式,还有其他类似的形式。例如,在我的程序中是“VerbFormsViewModel\u PastidIndicative”的过去指示性

所以我想要的是所有这些,但对于所有其他动词形式。我可以这样做的一种方法是复制并粘贴这个代码十到十二次,然后在绑定中更改细节

但是有更好的办法吗?如果可能的话,我更喜欢XAML解决方案。我的意思是,除了binding语句开头的部分之外,还有很多共享内容。点后面的位,例如“PlainPositive_TranslationEnglish”将是相同的


有一个原因,我有更多的堆栈面板比我似乎需要。我在这里发布的是两块文本框及其周围的控件。实际上每个动词形式需要八个;四行两行。

将相同的代码拉入UserControl,删除绑定的第一部分VerbFormsViewModel_PresentIndicative。然后使用该控件并将DataContext设置为viewmodel

控制:

使用控件


太好了,正是我想要的。谢谢
<UserControl x:Class="TranslationApp.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel Orientation="Horizontal">
        <StackPanel Margin="2">
            <TextBlock Margin="3" Text="{Binding PlainPositive_Name}" TextAlignment="Center" FontSize="12"
                       MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"/>
            <TextBlock Margin="2">English translation:</TextBlock>
            <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                     Text="{Binding PlainPositive_TranslationEnglish, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBlock Margin="2">Japanese kanji:</TextBlock>
            <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                     Text="{Binding PlainPositive_TranslationJapaneseKanji, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBlock Margin="2">Japanese hiragana:</TextBlock>
            <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                     Text="{Binding PlainPositive_TranslationJapaneseHiragana, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBlock Margin="2">Japanese romaji:</TextBlock>
            <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                     Text="{Binding PlainPositive_TranslationJapaneseRomaji, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        </StackPanel>

        <StackPanel Margin="2">
            <TextBlock Margin="3"  Text="{Binding PlainNegative_Name}" TextAlignment="Center" FontSize="12"
                       MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"/>
            <TextBlock Margin="2">English translation:</TextBlock>
            <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                     Text="{Binding PlainNegative_TranslationEnglish, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBlock Margin="2">Japanese kanji:</TextBlock>
            <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                     Text="{Binding PlainNegative_TranslationJapaneseKanji, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBlock Margin="2">Japanese hiragana:</TextBlock>
            <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                     Text="{Binding PlainNegative_TranslationJapaneseHiragana, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBlock Margin="2">Japanese romaji:</TextBlock>
            <TextBox Margin="2" MinWidth="{StaticResource StandardTBoxMinWidthWide}" MaxWidth="{StaticResource StandardTBoxMaxWidthWide}" TextWrapping="Wrap"
                     Text="{Binding PlainNegative_TranslationJapaneseRomaji, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        </StackPanel>

    </StackPanel>
</UserControl>
<Expander>
    <views:MyUserControl DataContext="{Binding VerbFormsViewModel_PresentIndicative}"
</Expander>