[英]WPF custom control with ItemsControl and ListBox
I am learning on how to create custom control in WPF. 我正在学习如何在WPF中创建自定义控件。 I have few problems that I am stuck with.
我遇到的问题很少。
Basically, I am trying to create custom control for navigation bar that has two level. 基本上,我试图为具有两个级别的导航栏创建自定义控件。
This is what I am trying to archieve: 这是我要存档的内容:
--------------------------------
| |
| 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... |
| |
--------------------------------
Here is my Generic.xaml 这是我的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>
Everything is working fine, but I have two problems: 一切正常,但是我有两个问题:
Thanks... 谢谢...
To detect which item gets clicked, you can bind SelectedItem in your ListBox. 若要检测单击哪个项目,可以在ListBox中绑定SelectedItem 。 eg;
例如; SelectedItem={Binding Property,Mode=TwoWay} So once you click on the items, In the setter you can raise notify property changed.
SelectedItem = {绑定属性,模式= TwoWay}因此,一旦单击项目,在设置器中就可以引发通知属性已更改。 You can also get the Item index from your Items collection.
您还可以从Items集合中获取Item索引。 For that you just need to bind SelectedIndex in your ListBox.
为此,您只需要在ListBox中绑定SelectedIndex 。
For 1 - 对于1-
Instead of Border, use a button with a control template that defines your image + text. 代替边框,使用带有控件模板的按钮定义您的图像和文本。
Create a ICommand property in the View model for each item, then bind it to the listbox item button's Command. 在视图模型中为每个项目创建一个ICommand属性,然后将其绑定到列表框项目按钮的命令。
Handle the click in the view-model. 处理视图模型中的单击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.