Specify ControlTemplate for ItemsControl.ItemContainerStyle

前端 未结 2 1543
不思量自难忘°
不思量自难忘° 2020-12-24 07:37

The following is similar to what I\'m trying to accomplish. However, I get the error

Invalid PropertyDescriptor value.

on the

2条回答
  •  温柔的废话
    2020-12-24 08:22

    You can qualify the property name with the type name:

    
    

    The container for ItemsControl is normally a ContentPresenter, but if the child is a UIElement then it won't use a container. In this case, all of the children are Controls, so the ItemContainerStyle will apply to them directly. If you added an item other than a UIElement, that setter would set the Control.Template property on the ContentPresenter, which would succeed but have no effect.

    Actually, it sounds like what you want is to wrap each child in a container, even if they are already a UIElement. To do that, you will have to use a subclass of ItemsControl. You could use an existing one like ListBox, or you could subclass ItemsControl and override GetContainerForItemOverride and IsItemItsOwnContainerOverride to wrap the items in your own container. You could wrap them in a ContentControl and then use that as the TargetType for the Style.

    public class CustomItemsControl
        : ItemsControl
    {
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new ContentControl();
        }
    
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            // Even wrap other ContentControls
            return false;
        }
    }
    

    You will also need to set the TargetType on the ControlTemplate so that the ContentPresenter will bind to the Content property:

    
    

提交回复
热议问题