【问题标题】:XAML DataTemplate with parameter带参数的 XAML 数据模板
【发布时间】:2020-09-27 16:35:01
【问题描述】:

我有一个数据模板是:

        <DataTemplate x:Key="CalDescButton">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock
                    Grid.Row="0"
                    Grid.RowSpan="2"
                    Grid.Column="0"
                    Grid.ColumnSpan="2"
                    Style="{StaticResource LargeIconStyle}"
                    Text="&#xEC92;" />
                <TextBlock
                    Grid.Row="0"
                    Grid.Column="1"
                    Foreground="Transparent"
                    Style="{StaticResource MediumIconStyle}"
                    Text="&#xEC92;" />
                <TextBlock
                    Grid.Row="1"
                    Grid.Column="1"
                    Foreground="Transparent"
                    Style="{StaticResource MediumIconStyle}"
                    Text="&#xEC92;" />
                <TextBlock
                    Grid.Row="0"
                    Grid.Column="1"
                    Foreground="Transparent"
                    Style="{StaticResource MediumIconStyle}"
                    Text="&#xEC92;" />
                <TextBlock
                    Grid.Row="1"
                    Grid.Column="0"
                    Foreground="Transparent"
                    Style="{StaticResource MediumIconStyle}"
                    Text="&#xEC92;" />
                <TextBlock
                    Grid.Row="1"
                    Grid.RowSpan="2"
                    Grid.Column="0"
                    Margin="0,-6,0,0"
                    Foreground="Aqua"
                    Style="{StaticResource XSmallIconStyle}"
                    Text="&#xF0AD;" />
            </Grid>
        </DataTemplate>

然后我还有其他 6 个,其中唯一改变的是文本字符代码。 这些模板应用于按钮:

               <ToggleButton
                    Command="{Binding CmdSetSorting, Mode=OneWay}"
                    CommandParameter="CalAsc"
                    ContentTemplate="{StaticResource CalDescButton}" />

问题是:我可以将变量传递给模板并在 Text="" 中使用它,而不是为此使用 6 个不同的模板吗? 我该怎么做?

为了清楚起见,我想要类似的东西:

<DataTemplate x:Key="IconButton">

然后:

ContentTemplate="{StaticResource IconButton (&#xF0AD;)}" />

【问题讨论】:

    标签: wpf xaml user-interface .net-core uwp-xaml


    【解决方案1】:

    设置Button的Content属性:

    <ToggleButton
        Command="{Binding CmdSetSorting, Mode=OneWay}"
        CommandParameter="CalAsc"
        Content="&#xEC92;"
        ContentTemplate="{StaticResource CalDescButton}" />
    

    它应该成为DataTemplate中根元素的DataContext(因为它是ContentTemplate),并且可以通过Binding访问:

    <TextBlock
        Grid.Row="0"
        Grid.RowSpan="2"
        Grid.Column="0"
        Grid.ColumnSpan="2"
        Style="{StaticResource LargeIconStyle}"
        Text="{Binding}" />
    

    【讨论】:

    • 为一个工作 :) 如果我必须通过 2?有什么建议吗?
    • @MassimoSavazzi,我会说最好创建一个CustomToggleButton : ToggleButton按钮控件,添加必要的依赖属性,并将ContentTemplate转换为ControlTemplate
    猜你喜欢
    • 1970-01-01
    • 2012-12-27
    • 2014-09-08
    • 1970-01-01
    • 2013-12-26
    • 2016-04-16
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多