How to add button in listbox in material design in XAML toolkit? #3093
-
|
Hi guys,
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
@VahidEra This question is not directly related to the MDIX toolkit, but it is a good question, and one that really highlights the flexibility that WPF provides. In WPF, all elements are associated with a template which dictates how the control should be rendered. So what you want to do here is to change the way a <ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource MaterialDesignListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type ContentControl}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- Panel of buttons -->
<StackPanel Grid.Column="0" Orientation="Horizontal">
<Button Content="{materialDesign:PackIcon Kind=Settings, Size=12}" Margin="2" Padding="4" Width="20" Height="20" />
<Button Content="{materialDesign:PackIcon Kind=Github, Size=12}" Margin="2" Padding="4" Width="20" Height="20" />
</StackPanel>
<!-- "Normal" cell content -->
<ContentPresenter Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Content="{TemplateBinding Content}" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<TextBlock Text="Plain" />
<TextBlock Text="Old" />
<TextBlock Text="ListBox" />
<TextBlock Text="Full of junk" />
</ListBox>The result of doing the above, will be something like this: |
Beta Was this translation helpful? Give feedback.


@VahidEra This question is not directly related to the MDIX toolkit, but it is a good question, and one that really highlights the flexibility that WPF provides. In WPF, all elements are associated with a template which dictates how the control should be rendered.
So what you want to do here is to change the way a
ListBoxItemis rendered, this can be done by overriding theListBox.ItemContainerStyleand modify it to fit your needs. The code below does so by inheriting from the normalMaterialDesignListBoxItemand then overriding theContentTemplateon that style to take control of the rendering of the actual content (this means the row highlight and other stuff is retained). In theConten…