Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何重用GridViewColumn CellTemplate并允许不同的绑定?_.net_Wpf_Xaml - Fatal编程技术网

.net 如何重用GridViewColumn CellTemplate并允许不同的绑定?

.net 如何重用GridViewColumn CellTemplate并允许不同的绑定?,.net,wpf,xaml,.net,Wpf,Xaml,我在WPF窗口中有一个ListView,其中包含许多GridViewColumns。第一列用于复选框。其余的列非常相似,包含一个带有textblock的datatemplate。我希望能够为每一列重用一个datatemplate,但我不确定如何实现这一点,因为每一列的绑定都不同 下面是一些XAML示例。第一个GridViewColumn是复选框。另外两个包含DataTemplate的示例。如何跨多个具有不同绑定的列重用此DataTemplate <ListView

我在WPF窗口中有一个ListView,其中包含许多GridViewColumns。第一列用于复选框。其余的列非常相似,包含一个带有textblock的datatemplate。我希望能够为每一列重用一个datatemplate,但我不确定如何实现这一点,因为每一列的绑定都不同

下面是一些XAML示例。第一个GridViewColumn是复选框。另外两个包含DataTemplate的示例。如何跨多个具有不同绑定的列重用此DataTemplate

        <ListView 
        AlternationCount="2" 
        DataContext="{StaticResource TaskGroups}" 
        ItemContainerStyle="{StaticResource TaskItemStyle}"
        ItemsSource="{Binding}"
        SelectionMode="Single">
        <ListView.View>
            <GridView>
                <GridViewColumn 
                    Header="Completed"
                    CellTemplate="{StaticResource CompletedCellTemplate}"
                />
                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Rectangle Name="StrikeThrough" HorizontalAlignment="Stretch" VerticalAlignment="Center"
                                   Height="1" StrokeThickness="1" Stroke="Transparent"/>
                                <TextBlock Text="{Binding Path=Name}"/>
                            </Grid>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=IsCompleted}" Value="True">
                                    <Setter TargetName="StrikeThrough" Property="Stroke" Value="Black"/>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Status">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Rectangle Name="StrikeThrough" HorizontalAlignment="Stretch" VerticalAlignment="Center"
                                   Height="1" StrokeThickness="1" Stroke="Transparent"/>
                                <TextBlock Text="{Binding Path=StatusDescription}"/>
                            </Grid>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=IsCompleted}" Value="True">
                                    <Setter TargetName="StrikeThrough" Property="Stroke" Value="Black"/>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

这些模板的唯一区别是显示的文本。所以,您可以创建用户控件来重用布局和删除逻辑

此外,还有
TextBlock.textdecommention
。最好使用它,而不是自定义的技巧

以下是所述控件的示例:

MyUserControl.xaml:

<UserControl x:Class="WpfApplication1.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" xmlns:WpfApplication1="clr-namespace:WpfApplication1" mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <TextDecoration x:Key="MyStrikeThrough" Location="Strikethrough"/>
        <WpfApplication1:BoolToTextDecorationConverter x:Key="BoolToTextDecorationConverter" Decoration="{StaticResource MyStrikeThrough}" />
    </UserControl.Resources>
    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Text}" TextDecorations="{Binding IsCompleted, Converter={StaticResource BoolToTextDecorationConverter}}" />
</UserControl>

这绝对是一个更优雅的解决方案。唯一的缺点是,当我使用用户控件时,gridviewcolumn不再延伸到其内容的宽度。你对如何实现这一目标有什么快速的建议吗?
using System.Windows.Controls;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MyUserControl.xaml
    /// </summary>
    public partial class MyUserControl : UserControl
    {
        public string Text { get; set; }

        public MyUserControl()
        {
            InitializeComponent();
        }
    }
}
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace WpfApplication1
{
    public class BoolToTextDecorationConverter : IValueConverter
    {
        public TextDecoration Decoration { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool && (bool)value)
            {
                return new TextDecorationCollection {Decoration};
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}