如何创建一个全局模板,其中可以从外部设置绑定属性



我的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>

但是,如何设置ListViewitemTextTextBoxContent属性?

我已经找到了与这个问题相关的其他文章,但没有设法解决它

如果你有其他建议,而不是使用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>

最新更新