Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
C# 如何在将自定义ListBox与自定义ListBoxItem一起使用时保留TemplateBindings_C#_Wpf_Xaml_Listbox_Custom Controls - Fatal编程技术网

C# 如何在将自定义ListBox与自定义ListBoxItem一起使用时保留TemplateBindings

C# 如何在将自定义ListBox与自定义ListBoxItem一起使用时保留TemplateBindings,c#,wpf,xaml,listbox,custom-controls,C#,Wpf,Xaml,Listbox,Custom Controls,我希望在自定义ListBox中使用我的自定义ListBoxItem,并希望能够在绑定到ItemSource时在ListBox数据模板中绑定我自己的属性 我什么也没发现。我发现的组合总是在Xaml声明的ItemContainerStyle或ItemTemplate样式中,但没有与我的情况类似的自定义类。也许我做错了什么 自定义列表框类: public class WPM_ListBox : ListBox { protected override bool IsItemItsOwnCont

我希望在自定义ListBox中使用我的自定义ListBoxItem,并希望能够在绑定到ItemSource时在ListBox数据模板中绑定我自己的属性

我什么也没发现。我发现的组合总是在Xaml声明的ItemContainerStyle或ItemTemplate样式中,但没有与我的情况类似的自定义类。也许我做错了什么

自定义列表框类:

public class WPM_ListBox : ListBox
{
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return (item is WPM_ListBoxItem);
    }

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

    public WPM_ListBox()
    {
        this.DefaultStyleKey = typeof(WPM_ListBox);
    }

    static WPM_ListBox()
    {

    }
}
这是WPM_ListBoxItem类的简短版本:

    public class WPM_ListBoxItem : ListBoxItem
{
    public WPM_ListBoxItem()
    {
        this.DefaultStyleKey = typeof(WPM_ListBoxItem);
    }
    static WPM_ListBoxItem()
    {
        BackgroundProperty.OverrideMetadata(typeof(WPM_ListBoxItem), 
            new FrameworkPropertyMetadata((SolidColorBrush)(new BrushConverter().ConvertFrom("#66767A"))));
    }

    public string WPM_HeaderContent
    {
        get { return (string)GetValue(WPM_HeaderContentProperty); }
        set { SetValue(WPM_HeaderContentProperty, value); }
    }
    public static readonly DependencyProperty WPM_HeaderContentProperty =
        DependencyProperty.Register("WPM_HeaderContent", typeof(string), 
        typeof(WPM_ListBoxItem), new PropertyMetadata("WPM_HeaderContent"));
}
以下是Generic.xaml:

<Style x:Key="ItemStyle" TargetType="local:WPM_ListBoxItem" >
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="Foreground" Value="#E9EEEE" x:Name="foreground"/>
    <Setter Property="IsHitTestVisible" Value="True"/>
    <Setter Property="Background" Value="Transparent" x:Name="Background_Property"/>
    <Setter Property="BorderBrush" Value="#67767A"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:WPM_ListBoxItem" >
                <local:WPM_Border IsHitTestVisible="True" Background="Transparent" Margin="1" VerticalAlignment="Top" x:Name="border" Theme="grau" BorderThickness5="1 1 0 0" BorderThickness4="1 1 0 0" BorderThickness3="1 1 0 0" BorderThickness2="1 1 0 0" BorderThickness1="1 1 0 0" >
                    <local:WPM_Border.Data>
                        <Grid x:Name="grid"  IsHitTestVisible="True" Background="Transparent" Margin="0" >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MinWidth="45" Width="Auto"/>
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <Grid.RowDefinitions>
                                <RowDefinition x:Name="row1" />
                                <RowDefinition x:Name="row2" />
                            </Grid.RowDefinitions>

                            <TextBlock VerticalAlignment="Top" FontFamily="Arial" FontWeight="Normal" FontSize="{TemplateBinding WPM_HeaderContenFontSize}" Margin="{TemplateBinding WPM_HeaderContentMargin}" Text="{TemplateBinding WPM_HeaderContent}" Foreground="{TemplateBinding WPM_HeaderContentBrush}" Padding="0 2 0 0" x:Name="headercontent" Grid.ColumnSpan="2" ScrollViewer.CanContentScroll="False"/>
                            <!--<ContentPresenter />-->

                            <StackPanel Margin="3 3 0 0" Grid.Row="1" x:Name="stackpanel1" >
                                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Top" FontWeight="Normal" FontSize="{TemplateBinding WPM_LabelDescriptionFontSize}" Margin="{TemplateBinding WPM_LabelDescriptionMargin}" Text="{TemplateBinding WPM_LabelDescription1}" Foreground="{TemplateBinding WPM_LabelDescriptionBrush1}"/>
                                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Top" FontWeight="Normal" FontSize="{TemplateBinding WPM_LabelDescriptionFontSize}" Margin="{TemplateBinding WPM_LabelDescriptionMargin}" Text="{TemplateBinding WPM_LabelDescription2}" Foreground="{TemplateBinding WPM_LabelDescriptionBrush2}"/>
                            </StackPanel>

                            <StackPanel Margin="3 3 0 0" Grid.Row="1" Grid.Column="1" x:Name="stackpanel2" >
                                <TextBlock VerticalAlignment="Top" FontWeight="Normal" FontSize="{TemplateBinding WPM_LabelContentFontSize}" HorizontalAlignment="Left" Margin="{TemplateBinding WPM_LabelContentMargin}" Text="{TemplateBinding WPM_LabelContent1}" Foreground="{TemplateBinding WPM_LabelContentBrush1}"/>
                                <TextBlock VerticalAlignment="Top" FontWeight="Normal" FontSize="{TemplateBinding WPM_LabelContentFontSize}" HorizontalAlignment="Left" Margin="{TemplateBinding WPM_LabelContentMargin}" Text="{TemplateBinding WPM_LabelContent2}" Foreground="{TemplateBinding WPM_LabelContentBrush2}"/>
                            </StackPanel>

                        </Grid>
                    </local:WPM_Border.Data>
                </local:WPM_Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter TargetName="row2" Property="Height" Value="Auto"/>
                        <Setter TargetName="border" Property="Height" Value="Auto"/>
                        <Setter Property="Height" Value="Auto"/>
                        <Setter TargetName="border" Property="BorderThickness1" Value="1 1 0 0"/>
                        <Setter TargetName="border" Property="BorderThickness2" Value="1 1 0 0"/>
                        <Setter TargetName="border" Property="BorderThickness3" Value="1 1 0 0"/>
                        <Setter TargetName="border" Property="BorderThickness4" Value="1 1 0 0"/>
                        <Setter TargetName="border" Property="BorderThickness5" Value="1 1 0 0"/>
                        <Setter TargetName="headercontent" Property="Padding" Value="3 2 0 0"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="false">
                        <Setter TargetName="row2" Property="Height" Value="0"/>
                        <Setter TargetName="border" Property="Height" Value="35"/>
                        <Setter Property="Height" Value="35"/>
                        <Setter TargetName="border" Property="BorderThickness1" Value="0"/>
                        <Setter TargetName="border" Property="BorderThickness2" Value="0"/>
                        <Setter TargetName="border" Property="BorderThickness3" Value="0"/>
                        <Setter TargetName="border" Property="BorderThickness4" Value="0"/>
                        <Setter TargetName="border" Property="BorderThickness5" Value="0"/>
                        <Setter TargetName="headercontent" Property="Padding" Value="0 2 0 0"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="BoxStyle" TargetType="local:WPM_ListBox">
    <Setter Property="Padding" Value="1"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="Once"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:WPM_ListBox">
                <Grid>
                    <ScrollViewer x:Name="PART_ScrollViewer"
                            IsTabStop="False"
                            Margin="0"
                            >
                        <ItemsPresenter/>
                    </ScrollViewer>
                    <ContentPresenter x:Name="PART_DropVisualPlaceholder" Visibility="Collapsed" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle" Value="{StaticResource ItemStyle}"></Setter>
</Style>

<Style TargetType="local:WPM_ListBoxItem" BasedOn="{StaticResource ItemStyle}"/>
<Style TargetType="local:WPM_ListBox" BasedOn="{StaticResource BoxStyle}"/>

在Xaml中,我想这样使用它:

<local:WPM_ListBox ItemsSource="{Binding RoutenListe}" Grid.Row="1" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0" Padding="0" Background="#32557B" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" >
                <local:WPM_ListBox.ItemTemplate  >
                    <DataTemplate >
                        <local:WPM_ListBoxItem WPM_HeaderContent="{Binding Name}" 
                                               WPM_LabelContent1="{Binding Startort}"  WPM_LabelContent2="{Binding Zielort}" 
                                               Background="Transparent" WPM_HeaderContentMargin="6 2 0 0" WPM_HeaderContenFontSize="24" WPM_LabelContentMargin="3 -4 0 3" 
                                               WPM_LabelDescription1="Start:"  WPM_LabelDescription2="Ziel:" WPM_LabelDescriptionMargin="10 -4 0 3" />
                    </DataTemplate>
                </local:WPM_ListBox.ItemTemplate>
            </local:WPM_ListBox>

我知道我可以用ContentPresenter替换声明的控件,然后只需在DataTemplate中使用标签或其他东西。但这不是依赖项的作用

如果我使用标准的ListBox并在DataTemplate中使用我的自定义ListBoxItem,则会显示我的数据(绑定数据),但“IsSelected”触发器不再工作。如果将自定义ListBox与DataTemplate中声明的自定义ListBoxItem一起使用,则只显示我声明的属性(声明的DependencyProperties中的标准值),而不显示绑定,但IsSelected属性有效

我甚至尝试将所有背景属性设置为透明,以确保HitTest不会失败


我遗漏了什么吗?

您使用的
ItemTemplate
错误
ItemTemplate
用于为ListBoxItem的内容提供模板,因此它不应包含ListBoxItem

选项1

从视图中删除
ItemTemplate

<local:WPM_ListBox ItemsSource="{Binding RoutenListe}" 
                   Grid.Row="1" 
                   HorizontalContentAlignment="Stretch" 
                   HorizontalAlignment="Stretch" 
                   BorderThickness="0" Margin="0" Padding="0" 
                   Background="#32557B" 
                   ScrollViewer.CanContentScroll="True" 
                   ScrollViewer.VerticalScrollBarVisibility="Auto" />
(编辑:如果要自定义某些特定项目的内容,需要在
WPM_ListBoxItem
的模板中使用
ContentPresenter
,然后使用
ItemTemplate
指定如何在该特定列中的
ContentPresenter
中显示数据)

选项2

不要覆盖
ListBox
的默认项目容器,而是将
WPM\u ListBoxItem
用作常规的
ContentControl
或类似内容,并将其添加到
ListBox
ItemTemplate
,更改绑定到实际
ListBoxItem
IsSelected
属性的
DataTriggers
Triggers
(现在是由ListBox生成的常规属性)

WPM_ListBoxItem.cs:

公共类WPM\u ListBoxItem:ContentControl
{
...
}
您的
WPM\u ListBoxItem
样式触发器:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="true">
        <Setter TargetName="row2" Property="Height" Value="Auto"/>
        <Setter TargetName="border" Property="Height" Value="Auto"/>
        <Setter Property="Height" Value="Auto"/>
        <Setter TargetName="border" Property="BorderThickness1" Value="1 1 0 0"/>
        <Setter TargetName="border" Property="BorderThickness2" Value="1 1 0 0"/>
        <Setter TargetName="border" Property="BorderThickness3" Value="1 1 0 0"/>
        <Setter TargetName="border" Property="BorderThickness4" Value="1 1 0 0"/>
        <Setter TargetName="border" Property="BorderThickness5" Value="1 1 0 0"/>
        <Setter TargetName="headercontent" Property="Padding" Value="3 2 0 0"/>
    </Trigger>
    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="false">
        <Setter TargetName="row2" Property="Height" Value="0"/>
        <Setter TargetName="border" Property="Height" Value="35"/>
        <Setter Property="Height" Value="35"/>
        <Setter TargetName="border" Property="BorderThickness1" Value="0"/>
        <Setter TargetName="border" Property="BorderThickness2" Value="0"/>
        <Setter TargetName="border" Property="BorderThickness3" Value="0"/>
        <Setter TargetName="border" Property="BorderThickness4" Value="0"/>
        <Setter TargetName="border" Property="BorderThickness5" Value="0"/>
        <Setter TargetName="headercontent" Property="Padding" Value="0 2 0 0"/>
    </Trigger>
</ControlTemplate.Triggers>

您的视图的XAML:

<ListBox ItemsSource="{Binding RoutenListe}" Grid.Row="1" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0" Padding="0" Background="#32557B" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto">
    <local:WPM_ListBox.ItemTemplate>
        <DataTemplate>
            <local:WPM_ListBoxItem WPM_HeaderContent="{Binding Name}" 
                                   WPM_LabelContent1="{Binding Startort}"  WPM_LabelContent2="{Binding Zielort}" 
                                   Background="Transparent" WPM_HeaderContentMargin="6 2 0 0" WPM_HeaderContenFontSize="24" WPM_LabelContentMargin="3 -4 0 3" 
                                   WPM_LabelDescription1="Start:"  WPM_LabelDescription2="Ziel:" WPM_LabelDescriptionMargin="10 -4 0 3" />
        </DataTemplate>
    </local:WPM_ListBox.ItemTemplate>
</ListBox>

您使用的
ItemTemplate
错误
ItemTemplate
用于为ListBoxItem的内容提供模板,因此它不应包含ListBoxItem

选项1

从视图中删除
ItemTemplate

<local:WPM_ListBox ItemsSource="{Binding RoutenListe}" 
                   Grid.Row="1" 
                   HorizontalContentAlignment="Stretch" 
                   HorizontalAlignment="Stretch" 
                   BorderThickness="0" Margin="0" Padding="0" 
                   Background="#32557B" 
                   ScrollViewer.CanContentScroll="True" 
                   ScrollViewer.VerticalScrollBarVisibility="Auto" />
(编辑:如果要自定义某些特定项目的内容,需要在
WPM_ListBoxItem
的模板中使用
ContentPresenter
,然后使用
ItemTemplate
指定如何在该特定列中的
ContentPresenter
中显示数据)

选项2

不要覆盖
ListBox
的默认项目容器,而是将
WPM\u ListBoxItem
用作常规的
ContentControl
或类似内容,并将其添加到
ListBox
ItemTemplate
,更改绑定到实际
ListBoxItem
IsSelected
属性的
DataTriggers
Triggers
(现在是由ListBox生成的常规属性)

WPM_ListBoxItem.cs:

公共类WPM\u ListBoxItem:ContentControl
{
...
}
您的
WPM\u ListBoxItem
样式触发器:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="true">
        <Setter TargetName="row2" Property="Height" Value="Auto"/>
        <Setter TargetName="border" Property="Height" Value="Auto"/>
        <Setter Property="Height" Value="Auto"/>
        <Setter TargetName="border" Property="BorderThickness1" Value="1 1 0 0"/>
        <Setter TargetName="border" Property="BorderThickness2" Value="1 1 0 0"/>
        <Setter TargetName="border" Property="BorderThickness3" Value="1 1 0 0"/>
        <Setter TargetName="border" Property="BorderThickness4" Value="1 1 0 0"/>
        <Setter TargetName="border" Property="BorderThickness5" Value="1 1 0 0"/>
        <Setter TargetName="headercontent" Property="Padding" Value="3 2 0 0"/>
    </Trigger>
    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="false">
        <Setter TargetName="row2" Property="Height" Value="0"/>
        <Setter TargetName="border" Property="Height" Value="35"/>
        <Setter Property="Height" Value="35"/>
        <Setter TargetName="border" Property="BorderThickness1" Value="0"/>
        <Setter TargetName="border" Property="BorderThickness2" Value="0"/>
        <Setter TargetName="border" Property="BorderThickness3" Value="0"/>
        <Setter TargetName="border" Property="BorderThickness4" Value="0"/>
        <Setter TargetName="border" Property="BorderThickness5" Value="0"/>
        <Setter TargetName="headercontent" Property="Padding" Value="0 2 0 0"/>
    </Trigger>
</ControlTemplate.Triggers>