WPF - How to use Badge in Mahapps HamburgerMenuItem - wpf

Quick question for who known WPF:
How to add a badge in a Mahapps HamburgerMenuItem?
I use this template:
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type Controls:HamburgerMenuIconItem}">
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ContentControl Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
<Rectangle Width="20" Height="20" HorizontalAlignment="Center" VerticalAlignment="Center">
<Rectangle.Fill>
<VisualBrush Stretch="Fill" Visual="{Binding Icon}" />
</Rectangle.Fill>
</Rectangle>
</ContentControl>
<TextBlock Grid.Column="1" VerticalAlignment="Center" FontSize="16" Foreground="White" Text="{Binding Label}" />
</Grid>
</DataTemplate>
And here is my Hamburger Items:
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuIconItem Icon="{StaticResource appbar_home_variant}" Label="Home">
<Controls:HamburgerMenuIconItem.Tag>
<Grid Name="HomeView"></Grid>
</Controls:HamburgerMenuIconItem.Tag>
</Controls:HamburgerMenuIconItem>
<Controls:HamburgerMenuIconItem Icon="{StaticResource appbar_people_status}" Label="Private" >
<Controls:HamburgerMenuIconItem.Tag>
<Grid Name="PrivateView"></Grid>
</Controls:HamburgerMenuIconItem.Tag>
</Controls:HamburgerMenuIconItem>
<Controls:HamburgerMenuIconItem Icon="{StaticResource appbar_page_onenote}" Label="Notes">
<Controls:HamburgerMenuIconItem.Tag>
<Grid Name="NotesView"></Grid>
</Controls:HamburgerMenuIconItem.Tag>
</Controls:HamburgerMenuIconItem>
<Controls:HamburgerMenuIconItem Icon="{StaticResource appbar_cog}" Label="Settings">
<Controls:HamburgerMenuIconItem.Tag>
<Grid Name="SettingsView"></Grid>
</Controls:HamburgerMenuIconItem.Tag>
</Controls:HamburgerMenuIconItem>
</Controls:HamburgerMenuItemCollection>
</Controls:HamburgerMenu.ItemsSource>
Here what I am looking for:
HamburgerMenu Badge
Regards,

As I found here
You can wrap them arround your Icon.
<Controls:Badged Badge="{Binding Path=BadgeValue}" BadgePlacementMode="BottomRight">
<!-- Control to wrap goes here -->
<Button>
<iconPacks:PackIconFontAwesome Kind="CommentOutline"/>
</Button>
</Controls:Badged>
In your example it would be:
<Controls:Badged Badge="{Binding Path=BadgeValue}">
<Controls:HamburgerMenuIconItem Icon="{StaticResource appbar_home_variant}" Label="Home">
<Controls:HamburgerMenuIconItem.Tag>
<Grid Name="HomeView"></Grid>
</Controls:HamburgerMenuIconItem.Tag>
</Controls:HamburgerMenuIconItem>
</Controls:Badged>

it is possible. But it will get cut and does not look very good IMO
Example closed:
Example opened:
How to get it?:
Just wrap your Icon in the DataTemplate with a Badge:
<DataTemplate x:Key="HamburgerMenuItem" DataType="{x:Type Controls:HamburgerMenuGlyphItem}">
<DockPanel Height="48" LastChildFill="True">
<Controls:Badged x:Name="IconPart"
Width="48"
Badge="?"
BadgeBackground="Red"
DockPanel.Dock="Left">
<Image Margin="12"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Source="{Binding Glyph}"
Stretch="UniformToFill" />
</Controls:Badged>
<TextBlock x:Name="TextPart"
VerticalAlignment="Center"
FontSize="16"
Text="{Binding Label}" />
</DockPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:HamburgerMenu}}, Path=PanePlacement}" Value="Right">
<Setter TargetName="IconPart" Property="DockPanel.Dock" Value="Right" />
<Setter TargetName="TextPart" Property="Margin" Value="8,0,0,0" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
You can modify this to your needs.
UPDATE: Here are additional information from the maintainer of MahApps: https://github.com/MahApps/MahApps.Metro/issues/3800#issuecomment-631103384
I hope this helps and happy coding
Tim

Related

can't move wrap panel and listbox to mainwindow

I have a popUp and inside this popUp there is a wrap panel and a listbox(even without wrappanel situation is the same) I want it to be inside the main window but I can't move it there! it returns off the margins, how to fix it?
<Window x:Class="MyHeroEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyHeroEditor"
Title="MainWindow" Height="635.075" Width="796.643"
WindowState="Maximized"
Background="Bisque">
<Window.Resources>
<Style TargetType="ListBox" x:Key="listbox" >
<Setter Property="Height" Value="250"/>
<Setter Property="Width" Value="250" />
</Style>
<Style TargetType="Button" x:Key="Button" >
<Setter Property="Height" Value="40"/>
<Setter Property="Width" Value="55" />
</Style>
<Style TargetType="WrapPanel" x:Key="Panel" >
<Setter Property="Height" Value="250"/>
<Setter Property="Width" Value="250" />
</Style>
<Style TargetType="Image" x:Key="Image" >
<Setter Property="Height" Value="70"/>
<Setter Property="Width" Value="70" />
</Style>
</Window.Resources>
<Window.DataContext>
<local:Collections/>
</Window.DataContext>
<Canvas Name="Hero" Drop="HeroDrop" AllowDrop="True" Margin="0,0,-8,-4"
>
<WrapPanel
x:Name="HelmetPanel" Style="{StaticResource Panel}" Height="Auto" Width="Auto" />
<Button
Click="Export" Style="{StaticResource Button}" Canvas.Left="264"
Canvas.Top="389" >Export
</Button>
<Button
Click="Open" Content="Open" Style="{StaticResource Button}" Canvas.Left="15" Canvas.Top="389" />
<Button
Click="Save" Style="{StaticResource Button}" Canvas.Left="179" Canvas.Top="389" >Save
</Button>
<Button
AllowDrop="True" Style="{StaticResource Button}" Click="Helmet_Button_Click" Canvas.Left="415" Canvas.Top="26" >Шлемы
</Button>
<Button
AllowDrop="True" Style="{StaticResource Button}" Click="Armature_Button_Click" Canvas.Left="524" Canvas.Top="26" >Броня
</Button>
<Button
AllowDrop="True" Style="{StaticResource Button}" Click="Weapon_Button_Click" Canvas.Left="615" Canvas.Top="26" >Оружие
</Button>
<Button
Click="Create" Content="Create" Style="{StaticResource Button}" Canvas.Left="90" Canvas.Top="389" />
<Button
AllowDrop="True" Style="{StaticResource Button}" Click="Gloves_Button_Click" Canvas.Left="727" Canvas.Top="26" Content="Перчатки" Width="59" />
<Button
AllowDrop="True" Style="{StaticResource Button}" Click="Boots_Button_Click" Canvas.Left="824" Canvas.Top="26" Content="Сапоги" />
<Popup
Name="HelmetsPopUp" StaysOpen="False" Placement="Mouse">
<ListBox x:Name="listhelmets" Style="{StaticResource listbox}" ItemsSource="{Binding ListHelmets}"
IsSynchronizedWithCurrentItem="True" PreviewMouseDown="helmet_MouseDown" MouseDown="helmet_MouseDown"
DragLeave="helmet_DragLeave" MouseUp="Listhelmets_OnMouseUp"
SelectedValuePath="protection" >
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Style="{StaticResource Image}"/>
<TextBox Height="30" Width="30" Text="{Binding protection}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Popup>
<Popup
Name="ArmaturePopUp" StaysOpen="False" Placement="Mouse" >
<ListBox x:Name="listarmature" Style="{StaticResource listbox}" ItemsSource="{Binding ListArmature}"
IsSynchronizedWithCurrentItem="True" MouseDown="armature_MouseDown" MouseUp="Listarmature_OnMouseUp"
PreviewMouseMove="armature_PreviewMouseMove" SelectedValuePath="protection" DragLeave="armature_DragLeave"
Canvas.Left="271" Canvas.Top="51">
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Style="{StaticResource Image}"/>
<TextBox Height="30" Width="30" Text="{Binding protection}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Popup>
<Popup
Name="WeaponPopUp" StaysOpen="False" Placement="Mouse" >
<WrapPanel
Name="WeaponPanel" Style="{StaticResource Panel}">
<ListBox x:Name="listweapons" Style="{StaticResource listbox}" ItemsSource="{Binding ListWeapon}"
IsSynchronizedWithCurrentItem="True" MouseDown="weapon_MouseDown" MouseUp="Listweapons_OnMouseUp"
DragLeave="weapons_DragLeave"
PreviewMouseMove="weapons_PreviewMouseMove" SelectedValuePath="attack">
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Style="{StaticResource Image}"/>
<TextBox Height="30" Width="30" Text="{Binding _attack}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</WrapPanel>
</Popup>
<TextBox x:Name="TextBox2" Canvas.Left="29" Canvas.Top="312" Height="55" Width="130" />
<Button
Click="Reset" Style="{StaticResource Button}" Content="reset" Canvas.Left="219" Canvas.Top="327" />
<WrapPanel Height="261" Width="264" Canvas.Left="29" Canvas.Top="15">
<ListBox x:Name="listHero" Height="237" Width="100" Style="{StaticResource listbox}" ItemsSource="{Binding ListHero}"
AllowDrop="True" >
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Style="{StaticResource Image}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</WrapPanel>
<Popup Name="GlovesPopUp" StaysOpen="False">
<Canvas>
<ListBox x:Name="listgloves" Height="237" Width="131" Style="{StaticResource listbox}" ItemsSource="{Binding ListGloves}"
AllowDrop="True" Canvas.Left="723" Canvas.Top="-519" >
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Style="{StaticResource Image}"/>
<TextBox Height="30" Width="30" Text="{Binding protection}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Canvas>
</Popup>
<Popup Name="BootsPopUp" StaysOpen="False">
<ListBox x:Name="listboots" Height="237" Width="137" Style="{StaticResource listbox}" ItemsSource="{Binding ListBoots}"
AllowDrop="True" Canvas.Left="634" Canvas.Top="104" >
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Style="{StaticResource Image}"/>
<TextBox Height="30" Width="30" Text="{Binding _attack}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Popup>
</Canvas>
</Window>
Try using the XAML editor instead of the Visual Editor as the Visual one tends to glitch in certain situations.
You should move your controls between these elements:
<Window x:Class="WPFSO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid>**Add your controls here**</Grid>
</Grid>
</Window>
My guess is you have something outside the second (inner) grid or a similar issue.
You can play with the following properties of Popup [or Tooltip] control to specify its position when it opens:
PlacementTarget (to change popup placement target):
Sample:
<Canvas Margin="5" Background="Red" Width="200" Height="150" >
<Ellipse Name="ellipse1"
Canvas.Top="60" Canvas.Left="50"
Height="85" Width="60"
Fill="Black"/>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=ellipse1}">
<TextBlock Background="LightBlue" FontSize="18">This is a Popup</TextBlock>
</Popup>
</Canvas>
Placement (Its value type is PlacementMode. A PlacementMode enumeration value that determines the orientation of the Popup control when the control opens, and that specifies how the control interacts with screen boundaries. The default is Bottom.)
Sample:
<Canvas Width="200" Height="150">
<Image Name="image1"
Canvas.Left="75"
Source="Water_lilies.jpg" Height="200" Width="200"/>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=image1}"
Placement="Bottom">
<TextBlock FontSize="14" Background="LightGreen">Placement=Bottom</TextBlock>
</Popup>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=image1}"
Placement="Top">
<TextBlock FontSize="14" Background="LightGreen">Placement=Top</TextBlock>
</Popup>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=image1}"
Placement="Left">
<TextBlock FontSize="14" Background="LightGreen">Placement=Left</TextBlock>
</Popup>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=image1}"
Placement="Right">
<TextBlock FontSize="14" Background="LightGreen">Placement=Right</TextBlock>
</Popup>
</Canvas>
HorizontalOffset
VerticalOffset
PlacementRectangle
Additional samples and info:
How to: Set the Placement Property for a Popup or Tooltip
Same problem and answer: Keep popup within window bounds:
Before:
After:

How can I disable all component and change background color?

I have a WPF application. When I press the exit button, I want to see the box like this:
The box name is Risultati. This works, but I want the all WPF application is disable and I can see a light gray how the modal dialog.
This is the code that I use to create my box, Risutlati:
<UserControl x:Class="ContattoOculare.RiepilogoEsercizio"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="342" d:DesignWidth="520">
<Grid Margin="0,0,0,0" Height="342" Width="520">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="35" />
<RowDefinition Height="40" />
<RowDefinition Height="100" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.Background>
<ImageBrush x:Name="backgroudCarta" ImageSource="./Resources/Esci_dal_gioco_Maschera.png"/>
</Grid.Background>
<!--INTESTAZIONE-->
<Grid Grid.Row="0" Margin="0,20,0,0" >
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="32" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Label Content="Risultati" VerticalAlignment="Bottom" HorizontalAlignment="Center"
FontSize="30" FontFamily="./Font/#Roboto-Bold" Foreground="White"/>
</Grid>
<Grid Grid.Row="1" Margin="0,0,0,14">
<Label Content="Contatto Oculare" VerticalAlignment="Top" HorizontalAlignment="Center"
FontSize="18" FontFamily="./Font/#Roboto-Bold" Foreground="White" Margin="190,-12,184,0"/>
</Grid>
</Grid>
<!--FINE INTESTAZIONE-->
<!--PRIMA RIGA-->
<Grid Grid.Row="1">
<Label Content="Tempo" FontSize="22"
FontFamily="./Font/#Roboto-Bold" Foreground="Gray" HorizontalAlignment="Center"/>
</Grid>
<!--FINE PRIMA RIGA-->
<!--SECONDA RIGA-->
<Grid Grid.Row="2" Margin="0,-12,0,0">
<Label x:Name="labelTempo" Content="0 sec" FontSize="25" FontWeight="Bold"
FontFamily="./Font/#Roboto-Bold" Foreground="Gray" HorizontalAlignment="Center"/>
</Grid>
<!--FINE SECONDA RIGA-->
<!--TERZA RIGA-->
<Grid Grid.Row="3" Margin="50,0,50,0">
<TextBox x:Name="textDescrizione" VerticalAlignment="Top" FontSize="25"
FontFamily="Calibri" Foreground="Gray"
TextWrapping="Wrap" AcceptsReturn="True" Height="100" />
</Grid>
<!--FINE TERZA RIGA-->
<!--QUARTA RIGA-->
<Grid Grid.Row="4" HorizontalAlignment="Center" Margin="0,5,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid Height="Auto" Width="Auto" HorizontalAlignment="Center"
Grid.Column="0" >
<Image Width="206" Height="46"
HorizontalAlignment="Center" VerticalAlignment="Bottom"
x:Name="save">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="./Resources/Tasto_Uscita gioco_Salva_Roll_ON.png"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="./Resources/Tasto_Uscita_gioco_Roll_OFF.png"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Label HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Roboto-Bold"
FontSize="20" Foreground="White">Salva</Label>
</Grid>
<Grid Height="Auto" Width="Auto" HorizontalAlignment="Center"
Grid.Column="1" >
<Image Width="206" Height="46"
HorizontalAlignment="Center" VerticalAlignment="Bottom"
x:Name="stop">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="./Resources/Tasto_Uscita_gioco_Salva_Roll_OFF.png"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="./Resources/Tasto_Uscita_gioco_Roll_OFF.png"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Label HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Roboto-Bold"
FontSize="20" Foreground="White">Annulla</Label>
</Grid>
</Grid>
<!--FINE QUARTA RIGA-->
</Grid>
</UserControl>
How can I do this?
For any WPF window, I normally will implement a mask over its content, something like:
<Window>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibility"/>
</Window.Resources>
<Grid>
<Grid>
<!--your window content-->
</Grid>
<!--this is your mask, give it a semi-transparent color-->
<Grid Background="#65000000" Visibility="{Binding IsModalDialogActived, Converter={StaticResource BooleanToVisibility}}"/>
<Grid>
<!--your dialog-->
</Grid>
</Grid>
</Window>
So that you can set the mask to Visible when your show the modal dialogue, and the mask will cast a shadow over the interface and prevent any user interactions with your application.
I would extend the Window class rather than the UserControl class. Then, call ShowDialog to show up your box. All other windows in the application will be disabled. That's a true (WPF) modal dialog, loosely coupled and reusable across your application.
Keep in mind:
Use AllowsTransparency="True" to allow semi-transparent colors for the Background.
Use WindowStyle="None" to remove the borders.
Use WindowState="Maximized" to occupy all available space.
Set the HorizontalAlignment, VerticalAlignment, Width, Height and Margin properties in your main Grid to place your box where desired.

How can I wrap a custom text?

I have a listbox that uses datatemplates and one of the elements in the template is a textblock. Problem is that the words won't wrap, and I don't want to set a fixed size. Anybody that knows how to resolve this problem? It's driving me crazy!
<ListBox Grid.Row=" 1" HorizontalContentAlignment="Stretch" Background="#24221f" ItemsSource="{Binding Messages}" ScrollViewer.VerticalScrollBarVisibility="Visible" ClipToBounds="False" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate >
<Border BorderBrush="#24221f" BorderThickness="3" Width=" auto">
<DockPanel Background="{StaticResource blackBackground}" HorizontalAlignment="Stretch" Width="auto">
<Border BorderThickness="3" BorderBrush="Transparent">
<Image Source="{Binding IconImageUrl}" VerticalAlignment="top" Height="22" Width ="22" DockPanel.Dock="Left" />
</Border>
<Border BorderThickness="3" BorderBrush="LightGray" Height="auto" Width="auto" HorizontalAlignment="Left" VerticalAlignment="Center" DockPanel.Dock="Left">
<Image Source="{Binding ProfileImageUrl}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="48" Width ="48" />
</Border>
<StackPanel Orientation="Vertical" DockPanel.Dock="Left" Margin="5,0,0,0">
<Label Content="{Binding Path=Sender}" Foreground="#feb41c" FontFamily="Verdana" FontWeight="Bold" FontSize="14" />
<TextBlock Width="100" Text="{Binding Path=ShortMessage}" Margin="10,0,0,0" Foreground="BlanchedAlmond" TextWrapping="Wrap" FontFamily="Verdana" />
<Label Content="{Binding Path=Time}" Margin="10,0,0,0" Foreground="DarkGray" FontFamily="Verdana" FontStyle="Italic"/>
</StackPanel>
</DockPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
StackPanel is Evil :=) when i have strange behaviours in a xaml which include StackPanel, switching to a Grid with right parameters ( fixed sized, or stars or "Auto" ) often fix the issue.
Note also that there is an error in your xaml since you set the DockPanel.Dock of your first image (IconImageUrl) whereas it is in the border that surrrounds it that you should be setting it. That may get the Layout to do strange things.
just try with HorizontalContentAlignment property to "Stretch" of ListBoxItems using the Style
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
and also disable the HorizontalScrollBar visibility
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Update
<Window.Resources>
<SolidColorBrush x:Key="blackBackground" Color="Black"/>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</Window.Resources>
<Grid>
<ListBox Grid.Row=" 1" HorizontalContentAlignment="Stretch" Background="#24221f" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Messages}" ScrollViewer.VerticalScrollBarVisibility="Visible" ClipToBounds="False" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate >
<Border BorderBrush="#24221f" BorderThickness="3" Width=" auto">
<DockPanel Background="{StaticResource blackBackground}" HorizontalAlignment="Stretch" Width="auto">
<Border BorderThickness="3" BorderBrush="Transparent">
<Image Source="{Binding IconImageUrl}" VerticalAlignment="top" Height="22" Width ="22" DockPanel.Dock="Left" />
</Border>
<Border BorderThickness="3" BorderBrush="LightGray" Height="auto" Width="auto" HorizontalAlignment="Left" VerticalAlignment="Center" DockPanel.Dock="Left">
<Image Source="{Binding ProfileImageUrl}" VerticalAlignment="Top" HorizontalAlignment="Left" Height="48" Width ="48" />
</Border>
<StackPanel Orientation="Vertical" DockPanel.Dock="Left" Margin="5,0,0,0">
<Label Content="{Binding Path=Sender}" Foreground="#feb41c" FontFamily="Verdana" FontWeight="Bold" FontSize="14" />
<TextBlock Text="{Binding Path=ShortMessage}" Margin="10,0,0,0" Foreground="BlanchedAlmond" TextWrapping="Wrap" FontFamily="Verdana" />
<Label Content="{Binding Path=Time}" Margin="10,0,0,0" Foreground="DarkGray" FontFamily="Verdana" FontStyle="Italic"/>
</StackPanel>
</DockPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I think this thread answer's your question, see the accespted answer by "Nash" - Force TextBlock to wrap in WPF ListBox
( and remember to upvote the answer the the linked thread if it helps you :) )

Setting Z-Index in DataBound items on a Canvas

I am trying to bind a list of items to an ItemsControl, using a Canvas layout, where the item has multiple "levels". This is easiest to explain using an image:
My lower level is a drop-shadow in this case. Because I assumed the drop shadow would be attached to the main element (a Button), I created another visual element, a Border, which sits behind the Button and has the shadow attached. What I would like is for all of my shadow elements to be at the same overall ZIndex, and all of the Button elements to be above them.
In practice, it appears that WPF renders the contents of my template as a single UI element, essentially flattening the ZIndexes within it. Is there any way I can make it so that the ZIndex values are not flattened?
I have created a sample below which shows the problem:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="250" Width="600" Background="White">
<Window.Resources>
<DropShadowEffect Color="Blue" BlurRadius="75" x:Key="ActionDropShadow" />
<XmlDataProvider x:Key="myData" XPath="Data/Items">
<x:XData>
<Data xmlns="">
<Items>
<Item X="50" Title="AAA" />
<Item X="100" Title="BBB" />
<Item X="150" Title="CCC" />
</Items>
</Data>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="BoxTemplate">
<Grid>
<Border Background="Black" BorderThickness="1" Effect="{StaticResource ActionDropShadow}" />
<Button Background="White" BorderThickness="1">
<TextBlock Text="{Binding XPath=#Title}" />
</Button>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ItemsControl Grid.Column="0" x:Name="list" ItemTemplate="{StaticResource BoxTemplate}" ItemsSource="{Binding Source={StaticResource myData},XPath=*}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding XPath=#X}" />
<Setter Property="Canvas.Top" Value="50" />
<Setter Property="Width" Value="50" />
<Setter Property="Height" Value="80" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
<Canvas Grid.Column="1">
<Border Panel.ZIndex="5" Canvas.Top="50" Canvas.Left="50" Width="50" Height="80" Background="Black" BorderThickness="1" Effect="{StaticResource ActionDropShadow}" />
<Button Panel.ZIndex="10" Canvas.Top="50" Canvas.Left="50" Width="50" Height="80" Background="White" BorderThickness="1">
<TextBlock Text="AAA" />
</Button>
<Border Panel.ZIndex="5" Canvas.Top="50" Canvas.Left="100" Width="50" Height="80" Background="Black" BorderThickness="1" Effect="{StaticResource ActionDropShadow}" />
<Button Panel.ZIndex="10" Canvas.Top="50" Canvas.Left="100" Width="50" Height="80" Background="White" BorderThickness="1">
<TextBlock Text="BBB" />
</Button>
<Border Panel.ZIndex="5" Canvas.Top="50" Canvas.Left="150" Width="50" Height="80" Background="Black" BorderThickness="1" Effect="{StaticResource ActionDropShadow}" />
<Button Panel.ZIndex="10" Canvas.Top="50" Canvas.Left="150" Width="50" Height="80" Background="White" BorderThickness="1">
<TextBlock Text="CCC" />
</Button>
</Canvas>
<TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center">Databinding Version</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center">What it should look like</TextBlock>
</Grid>
</Window>
Thanks in advance for any ideas you can offer!
Since the bound objects are wrapped in a ContentPresenter all internal ZIndex settings will be ignored, so you cannot do what you described. You might be better off creating an ItemsControl for each layer.
Get the parent ContentPresenter and set ZIndex on that.
E.g:
var parent = (UIElement) VisualTreeHelper.GetParent((UIElement) sender);
parent.SetZIndex(parent, z);

WPF: ComboBoxItem background

I have a ComboBox based on XML file data:
<Root>
<Node Background="Yellow" Foreground="Cyan" Image="1.ico" Property="aaaa" Value="28" />
<Node Background="SlateBlue" Foreground="Black" Image="2.ico" Property="bbbb" Value="2.5" />
<Node Background="Teal" Foreground="Green" Image="3.ico" Property="cccc" Value="4.0" />
<Node Background="Yellow" Foreground="Red" Image="4.ico" Property="dddd" Value="0" /></Root>
So, in this case, I need to create a compound ComboBoxItem when each item has a suitable background.
I tried to do something like this:
<UserControl.Resources>
<DataTemplate DataType="Node">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="20"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" MinWidth="20"/>
</Grid.ColumnDefinitions>
<Border Background="{Binding XPath=#Background}" Grid.Column="0">
<Image Source="{Binding XPath=#Image}"
Width="16"
Height="16"
Margin="3" />
</Border>
<Border Background="{Binding XPath=#Background}" Grid.Column="1">
<TextBlock Foreground="{Binding XPath=#Foreground}"
Margin="3"
Text="{Binding XPath=#Property}" />
</Border>
<Border Background="{Binding XPath=#Background}" Grid.Column="2">
<TextBlock Foreground="{Binding XPath=#Foreground}"
Margin="3"
FontWeight="Bold"
Text="{Binding XPath=#Value}" />
</Border>
</Grid>
</DataTemplate>
<XmlDataProvider x:Key="xmlNodeList"
Source="/data/Combo.xml"
XPath="/Root/Node"/>
</UserControl.Resources>
<ComboBox Name="myComboBox"
ItemsSource="{Binding Source={StaticResource xmlNodeList}}"
SelectedIndex="0" />
but it's not looks good :-(
What solution you recommend?
Thanks in advance!
One thing is that you forgot to indicate which template to use:
<DataTemplate x:Key="Node">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="20"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" MinWidth="20"/>
</Grid.ColumnDefinitions>
<Border Background="{Binding XPath=#Background}" Grid.Column="0">
<Image Source="{Binding XPath=#Image}"
Width="16"
Height="16"
Margin="3" />
</Border>
<Border Background="{Binding XPath=#Background}" Grid.Column="1">
<TextBlock Foreground="{Binding XPath=#Foreground}"
Margin="3"
Text="{Binding XPath=#Property}" />
</Border>
<Border Background="{Binding XPath=#Background}" Grid.Column="2">
<TextBlock Foreground="{Binding XPath=#Foreground}"
Margin="3"
FontWeight="Bold"
Text="{Binding XPath=#Value}" />
</Border>
</Grid>
</DataTemplate>
And then your ComboBox:
<ComboBox Name="myComboBox"
ItemsSource="{Binding Source={StaticResource xmlNodeList}}"
ItemTemplate="{StaticResource Node}"
SelectedIndex="0" />
The "dataType" system works with typed object, I'm not sure you can make it work with XML data. This will work.
Update:
Before you ask, you should also define a Style for the items so that they cove the width of the list, otherwise your columns are going to be uneven:
<ComboBox Name="myComboBox"
ItemsSource="{Binding Source={StaticResource xmlNodeList}}"
ItemTemplate="{StaticResource Node}"
SelectedIndex="0">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
This will stretch the individual items to cover the whole width of the list.

Resources