1

我正在尝试装饰一个 ItemsControl,以便每个项目都有一个 Delete 按钮,该按钮在受 iPhone UI 启发的特定条件下浮动在项目的内容上。我有几种方法可以解决这个问题,但我可以使用其他 WPF 人员的一些指导,他们可能对如何最好地做到这一点有更好的了解。下面是一个模拟图像,以帮助表达我正在尝试做的事情。

样机示例

我目前的想法是尝试通过仅使用样式、模板以及必要时附加的属性仅使用 XAML 来尝试此操作。这个想法是为项目控件创建条件 DataTemplate,它会以某种方式使用包含我的删除按钮的装饰器来包装原始内容。为了在我的 ItemsControl 上有一个状态以知道我是否处于删除模式,我正在考虑创建一个附加属性,然后我可以通过多种方式设置该属性,例如将其绑定到状态例如切换按钮或复选框。

在这一点上,这个概念是有道理的,但细节对我来说有点不清楚是否使用 ItemTemplate 是最好的举动,因为在某些情况下,给定 ItemsControl 的 ItemTemplate 可能已经存在,我不想覆盖它,而是只会想要包装它(如果这有意义的话)。我在想,如果我成功了,我应该可以通过指定样式和附加属性来将其应用于任何项目控件。

如果有人可以帮助说明这些更详细的细节或提供有关我如何进行的更好的建议,请分享。

4

2 回答 2

2

通常,您会在 ItemContainerStyle 中添加类似的内容,以免每次要应用时都弄乱 DataTemplate 本身。虽然使用 ListBox 很简单,您可以在其中修改 ListBoxItem 的模板,但不幸的是,基本 ItemsControl 仅使用 ContentPresenter 作为其容器,因此不能以相同的方式进行模板化。

如果您真的希望它是可重用的,我建议您将它包装到一个新的自定义 ItemsControl 中,您可以放入其中替换标准的,而无需修改正在使用的特定 DataTemplate。这也将允许您将原本在外部创建的属性作为附加道具和控件本身中的删除命令包装起来。

显然删除逻辑和视觉样式还没有在这里完成,但这应该让你开始:

public class DeleteItemsControl : ItemsControl
{
    public static readonly DependencyProperty CanDeleteProperty = DependencyProperty.Register(
        "CanDelete",
        typeof(bool),
        typeof(DeleteItemsControl),
        new UIPropertyMetadata(null));

    public bool CanDelete
    {
        get { return (bool)GetValue(CanDeleteProperty); }
        set { SetValue(CanDeleteProperty, value); }
    }

    public static RoutedCommand DeleteCommand { get; private set; }

    static DeleteItemsControl()
    {
        DeleteCommand = new RoutedCommand("DeleteCommand", typeof(DeleteItemsControl));
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DeleteItemsControl), new FrameworkPropertyMetadata(typeof(DeleteItemsControl)));
    }

    protected override DependencyObject GetContainerForItemOverride()
    {
        return new DeleteItem();
    }

    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return item is DeleteItem;
    }
}

public class DeleteItem : ContentControl
{
    static DeleteItem()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DeleteItem), new FrameworkPropertyMetadata(typeof(DeleteItem)));
    }

}

这将放在 Generic.xaml 中,或者您可以像在您的应用程序中一样应用它们:

<Style TargetType="{x:Type local:DeleteItemsControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:DeleteItemsControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ItemsPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type local:DeleteItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:DeleteItem}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <DockPanel>
                        <Button Command="local:DeleteItemsControl.DeleteCommand" Content="X" HorizontalAlignment="Left" VerticalAlignment="Center"
                            Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:DeleteItemsControl}}, Path=CanDelete, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                        <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                    </DockPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2010-01-21T21:29:27.437 回答
1

我创建了以下模型来说明可以完成此操作的一种简单方法,即使用单个DataTemplate几个绑定。你绝对是在正确的轨道上。

<Window x:Class="TestWpfApplication.Foods"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApplication"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Foods" ResizeMode="NoResize"
SizeToContent="WidthAndHeight"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <local:BoolToVisConverter x:Key="BoolToVisConverter"/>
</Window.Resources>
<StackPanel Background="LightGray">
    <ToggleButton Name="EditModeToggle" Content="Edit" HorizontalAlignment="Right" FontFamily="Arial" Padding="4" 
                  Background="#7FA4E6" Foreground="White" BorderBrush="Black" Width="60" Margin="5,5,5,0"/>
    <ListBox ItemsSource="{Binding Items}" 
             Background="#999" BorderBrush="Black" Margin="5">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border CornerRadius="8" Background="#3565BC" Padding="8" 
                        BorderBrush="#333" BorderThickness="1" Margin="2" Width="255">
                    <StackPanel Orientation="Horizontal">
                        <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=RemoveItemCommand}"
                                Visibility="{Binding ElementName=EditModeToggle, Path=IsChecked, Converter={StaticResource BoolToVisConverter}}">
                            <Button.Content>
                                <TextBlock Foreground="Red" Text="X" FontWeight="Bold"/>
                            </Button.Content>
                        </Button>
                        <TextBlock Text="{Binding}" Margin="12,0" Foreground="#AAA" VerticalAlignment="Center"
                                   FontSize="14" FontWeight="Bold" FontFamily="Arial"/>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

您不需要太多代码来完成这项工作,但仍有一些代码隐藏,主要用于RoutedCommand

public partial class Foods : Window
{
    private ObservableCollection<String> items = new ObservableCollection<string>();
    private RoutedCommand removeItemCommand = new RoutedCommand();

    public Foods()
    {
        InitializeComponent();

        items.Add("Ice Cream");
        items.Add("Pizza");
        items.Add("Apple");

        CommandBindings.Add(new CommandBinding(removeItemCommand, ExecutedRemoveItem));
    }

    public ObservableCollection<String> Items
    {
        get { return items; }
    }

    public RoutedCommand RemoveItemCommand
    {
        get { return removeItemCommand; }
    }

    private void ExecutedRemoveItem(object sender, ExecutedRoutedEventArgs e)
    {
        DependencyObject container = 
            ItemsControl.ContainerFromElement(e.Source as ItemsControl, e.OriginalSource as DependencyObject);
        ListBoxItem item = container as ListBoxItem;
        items.Remove(item.Content as String);
    }
}

显然,结果可以在视觉上更具吸引力,但我几乎复制了你的想法哈哈:

替代文字 http://img697.imageshack.us/img697/7033/foodswindow.png

于 2010-01-21T20:42:57.553 回答