我的UserControl.xaml
文件中有以下标记:
<Grid DataContext="{Binding ElementName=ControlForProjectSettings, Path=ViewModel}"
ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource HeadingTextBlocksStyle}"
Text="Project settings"/>
<TextBlock Grid.Row="1" Grid.Column="0" Style="{StaticResource StandardTextBlocksStyle}"
Text="Project Number"/>
<TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource DefaultTextBoxesStyle}"
Text="{Binding ProjectNumber,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Style="{StaticResource StandardTextBlocksStyle}"
Text="Customer Name"/>
<TextBox Grid.Row="2" Grid.Column="1" Style="{StaticResource DefaultTextBoxesStyle}"
Text="{Binding CustomerName,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
我在其他用户控件中使用了类似的方法,我想在其中显示一个TextBlock和一个TextBox。
现在我想摆脱重复的代码,并使用一个模板的代码块如下:
<TextBlock Grid.Row="1" Grid.Column="0" Style="{StaticResource StandardTextBlocksStyle}"
Text="Project Number"/>
<TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource DefaultTextBoxesStyle}"
Text="{Binding ProjectNumber,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
因此,我的想法是创建一个ControlTemplate
,它看起来像这样:
<ControlTemplate x:Key="OneSettingsEntry">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource StandardTextBlocksStyle}" Text="{TemplateBinding Text}" />
<TextBox Style="{StaticResource DefaultTextBoxesStyle}"
Text="{TemplateBinding TextBoxContent}" />
</StackPanel>
</ControlTemplate>
以及我想在UserControl.xaml
中使用的模板,但也在其他User Controls
:中使用
<Grid DataContext="{Binding ElementName=ControlForProjectSettings, Path=ViewModel}">
<StackPanel>
<ListView>
<ListViewItem Template="{StaticResource OneSettingsEntry }"/>
</ListView>
</StackPanel>
</Grid>
但是,如何设置ListViewitem
的Text
或TextBoxContent
属性?
我已经找到了与这个问题相关的其他文章,但没有设法解决它
如果你有其他建议,而不是使用ControlTemplate
,这也可以解决我的问题,那么就启用它
如果您不受ControlTemplate
的限制,我会使用DataTemplate
在Resources
中定义数据模板
<Window.Resources>
<DataTemplate x:Key="YourViewModelTemplate" DataType="{x:Type local:YourViewModel}">
<Grid
ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource HeadingTextBlocksStyle}"
Text="Project settings"/>
<TextBlock Grid.Row="1" Grid.Column="0" Style="{StaticResource StandardTextBlocksStyle}"
Text="Project Number"/>
<TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource DefaultTextBoxesStyle}"
Text="{Binding ProjectNumber,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Style="{StaticResource StandardTextBlocksStyle}"
Text="Customer Name"/>
<TextBox Grid.Row="2" Grid.Column="1" Style="{StaticResource DefaultTextBoxesStyle}"
Text="{Binding CustomerName,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</DataTemplate>
</Window.Resources>
使用DataTemplate
作为ListView
。
<ListView ItemTemplate="{StaticResource YourViewModelTemplate}">
</ListView>