列表框项目上的控件模板存在问题
我在列表框项目上有自己的样式,如下:
<Style x:Key="friendsListStyle" TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid Name="RootLayout">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
</Grid.RowDefinitions>
<Image Margin="4,4,4,2" Grid.Column="0">
<Image.Source >
<MultiBinding Converter="{StaticResource avatarConverter}">
<Binding Path="ProfilePhoto"></Binding>
<Binding Path="StatusInfo.IsLogged"></Binding>
</MultiBinding>
</Image.Source>
</Image>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock
Text="{Binding Path=Nick}"
Margin="2,2,2,2"
FontSize="13"
FontWeight="Medium"
Grid.Column="0" Grid.Row="0">
</TextBlock>
<TextBlock
Text="{Binding Path=StatusMessageInfo.Message}"
FontSize="11"
FontWeight="Normal"
Foreground="DarkGray"
Grid.Column="0" Grid.Row="1" Margin="2,2,2,2"></TextBlock>
<TextBlock
Style="{StaticResource StatusStyle}"
Grid.Column="0" Grid.Row="2" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource infoConverter}">
<Binding Path="StatusInfo.IsLogged"></Binding>
<Binding Path="StatusInfo.IsChating"></Binding>
<Binding Path="StatusInfo.RoomName"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
在应用程序中如下所示:
我需要更改列表框的颜色项目时被选中,所以我尝试在列表框项目上编写控件模板并在列表框样式中使用:
这是列表框项目上的控件模板:
<Style x:Key="FriendListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Name="RootLayout">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
</Grid.RowDefinitions>
<Image Margin="4,4,4,2" Grid.Column="0">
<Image.Source >
<MultiBinding Converter="{StaticResource avatarConverter}">
<Binding Path="ProfilePhoto"></Binding>
<Binding Path="StatusInfo.IsLogged"></Binding>
</MultiBinding>
</Image.Source>
</Image>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock
Text="{Binding Path=Nick}"
Margin="2,2,2,2"
FontSize="13"
FontWeight="Medium"
Grid.Column="0" Grid.Row="0">
</TextBlock>
<TextBlock
Text="{Binding Path=StatusMessageInfo.Message}"
FontSize="11"
FontWeight="Normal"
Foreground="DarkGray"
Grid.Column="0" Grid.Row="1" Margin="2,2,2,2"></TextBlock>
<TextBlock
Style="{StaticResource StatusStyle}"
Grid.Column="0" Grid.Row="2" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource infoConverter}">
<Binding Path="StatusInfo.IsLogged"></Binding>
<Binding Path="StatusInfo.IsChating"></Binding>
<Binding Path="StatusInfo.RoomName"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsSelected" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Yellow" />
</MultiTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
应用在列表框样式上:
<Style x:Key="FriendListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="ItemContainerStyle" Value="{DynamicResource FriendListBoxItemStyle}" />
</Style>
最后在视图中的控件上应用列表框样式:
<ListBox Name="Friends"
SelectedIndex="{Binding Path=SelectedFriendsIndex,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=SelectedFriend, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource FriendListBoxStyle}"/>
我运行应用程序,列表框看起来像这里:
列表框中的项目也是不可选择的,所以我无法选择列表框中的项目。什么是坏事?
I have own style on listbox item, here is it:
<Style x:Key="friendsListStyle" TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid Name="RootLayout">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
</Grid.RowDefinitions>
<Image Margin="4,4,4,2" Grid.Column="0">
<Image.Source >
<MultiBinding Converter="{StaticResource avatarConverter}">
<Binding Path="ProfilePhoto"></Binding>
<Binding Path="StatusInfo.IsLogged"></Binding>
</MultiBinding>
</Image.Source>
</Image>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock
Text="{Binding Path=Nick}"
Margin="2,2,2,2"
FontSize="13"
FontWeight="Medium"
Grid.Column="0" Grid.Row="0">
</TextBlock>
<TextBlock
Text="{Binding Path=StatusMessageInfo.Message}"
FontSize="11"
FontWeight="Normal"
Foreground="DarkGray"
Grid.Column="0" Grid.Row="1" Margin="2,2,2,2"></TextBlock>
<TextBlock
Style="{StaticResource StatusStyle}"
Grid.Column="0" Grid.Row="2" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource infoConverter}">
<Binding Path="StatusInfo.IsLogged"></Binding>
<Binding Path="StatusInfo.IsChating"></Binding>
<Binding Path="StatusInfo.RoomName"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
In app look like this:
I need change color of listbox item when is selected, so I try wrote control template on listbox item and use in listbox style:
Here is control template on listbox item:
<Style x:Key="FriendListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Name="RootLayout">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
</Grid.RowDefinitions>
<Image Margin="4,4,4,2" Grid.Column="0">
<Image.Source >
<MultiBinding Converter="{StaticResource avatarConverter}">
<Binding Path="ProfilePhoto"></Binding>
<Binding Path="StatusInfo.IsLogged"></Binding>
</MultiBinding>
</Image.Source>
</Image>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock
Text="{Binding Path=Nick}"
Margin="2,2,2,2"
FontSize="13"
FontWeight="Medium"
Grid.Column="0" Grid.Row="0">
</TextBlock>
<TextBlock
Text="{Binding Path=StatusMessageInfo.Message}"
FontSize="11"
FontWeight="Normal"
Foreground="DarkGray"
Grid.Column="0" Grid.Row="1" Margin="2,2,2,2"></TextBlock>
<TextBlock
Style="{StaticResource StatusStyle}"
Grid.Column="0" Grid.Row="2" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource infoConverter}">
<Binding Path="StatusInfo.IsLogged"></Binding>
<Binding Path="StatusInfo.IsChating"></Binding>
<Binding Path="StatusInfo.RoomName"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsSelected" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Yellow" />
</MultiTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Apply on listBox style:
<Style x:Key="FriendListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="ItemContainerStyle" Value="{DynamicResource FriendListBoxItemStyle}" />
</Style>
An finally apply listbox style on control in view:
<ListBox Name="Friends"
SelectedIndex="{Binding Path=SelectedFriendsIndex,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=SelectedFriend, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource FriendListBoxStyle}"/>
I run app and listbox look as here:
Also items in listbox are not selectable, so I can’t select item in listbox. What is bad?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所做的是将
ItemContainerStyle
与ItemTemplate
混合。您需要做的是:
Blend
或ShowMeTheTemplate
提取ListBoxItem
模板,并添加Trigger
来更改其背景选择时的颜色。ListBox
的ItemTemplate
属性的DataTemplate
中。What you have done is mix
ItemContainerStyle
withItemTemplate
.What you need to do is:
ListBoxItem
template usingBlend
orShowMeTheTemplate
and add aTrigger
to change it's Background color when it is selected.DataTemplate
assigned toItemTemplate
property of theListBox
.