我正在学习如何在WPF中创建自定义控件。我遇到的问题很少。
基本上,我正在尝试为具有两个级别的导航栏创建自定义控件。
这就是我想要实现的目标:
--------------------------------
| |
| ICON TITLE 1 |
| |
| small icon option 1 |
| small icon option 2 |
| small icon option 3 |
| |
| |
| ICON TITLE 2 |
| |
| small icon option 1 |
| small icon option 2 |
| etc... |
| |
--------------------------------
这是我的 Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Global.WPFs.GUIs">
<Style TargetType="{x:Type local:GNavBar}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:GNavBar}">
<ScrollViewer x:Name="PART_Scroll"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
VerticalScrollBarVisibility="Auto"
Focusable="False">
<ItemsControl x:Name="PART_Items">
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Margin" Value="0"/>
<Setter Property="Control.Padding" Value="0 8 0 2"/>
<Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="76"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="76" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding ImgSrc}" Width="72" Height="72" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
<TextBlock Grid.Column="1" Text="{Binding Text}" Margin="4 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="20"></TextBlock>
</Grid>
<ListBox ItemsSource="{Binding Items}"
BorderThickness="0"
Background="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Margin" Value="0"/>
<Setter Property="Control.Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="36"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16" />
<ColumnDefinition Width="16" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="1" Source="{Binding ImgSrc}" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
<TextBlock Grid.Column="2" Text="{Binding Text}" Margin="4 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16"></TextBlock>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
一切正常,但我有两个问题:
答案 0 :(得分:1)
要检测单击的项目,可以在ListBox中绑定 SelectedItem 。 例如; SelectedItem = {Binding Property,Mode = TwoWay} 因此,一旦您单击项目,在setter中您可以提高通知属性已更改。 您还可以从Items集合中获取Item索引。为此,您只需要在ListBox中绑定 SelectedIndex 。
答案 1 :(得分:0)
1 -
使用带有控件模板的按钮代替边框,该控件模板定义图像+文本。
在View模型中为每个项目创建一个ICommand属性,然后将其绑定到列表框项目按钮的Command。
处理视图模型中的点击。