【问题标题】:How to create a global template where binding properties can be set from outside如何创建可以从外部设置绑定属性的全局模板
【发布时间】:2020-12-09 17:36:31
【问题描述】:

我的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 设置TextTextBoxContent 属性?

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

如果您有其他建议而不是使用 ControlTemplate,这也可以解决我的问题,然后提出来。

【问题讨论】:

    标签: c# wpf xaml datatemplate controltemplate


    【解决方案1】:

    如果您不受ControlTemplate 的限制,我会使用DataTemplate

    Resources 中定义DataTemplate

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

    【讨论】:

    • 感谢您的建议,我也可以使用DataTemplate。但我想在App.xaml 中使用数据模板并制作绑定属性,例如ProjectNumber 一种变量,所以我可以在使用模板时设置它们。那是因为我还有其他User Controls,我也想在其中使用该模板,但具有不同的绑定属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多