我不认为我在标题中说得很好,所以让我解释一下。首先,它是一个使用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的对象的绑定。这代表的是动词的形式,还有其他形式。例如,过去的指示是&#39; VerbFormsViewModel_PastIndicative&#39;在我的计划中。
所以我想要的是拥有所有这些,但是对于所有其他动词形式。我可以这样做的一种方法是只复制和粘贴此代码十次或十二次,只需更改绑定中的详细信息即可。
但是有更好的方法吗?如果可能的话,我更喜欢XAML解决方案。我的意思是除了绑定语句开头的部分之外,还有很多共享。点后面的位(例如&#39; PlainPositive_TranslationEnglish&#39;)将是相同的。
(有一个原因我有更多的堆叠面板比我看起来需要的。我在这里发布的是两个文本框及其周围的控件。实际上每个动词形式需要八个;两行四个。)
答案 0 :(得分:1)
将相同的代码拉入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>