WPF ListView: items with and without icon - wpf

How to create a ListView in WPF so that some ListViewItems have icon and some not? I'd like to achive the ListView as in the below image.
I started with the below code but didn't get how to set the icon to the first 4 items only but not to other items. If the item has no icon, its text should left-align with any icon as shown in the image.
I also need to give the items with no icon slightly higher height than the items with icon.
<ListView>
<ListView.Items>
<ListViewItem Content="Save" />
<ListViewItem Content="Save As" />
<ListViewItem Content="Open" />
<ListViewItem Content="Close" />
<ListViewItem Content="Info" />
<ListViewItem Content="Recent" />
<ListViewItem Content="New" />
</ListView.Items>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
' ???
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

Though i agree that this seems like a Menu and not a ListView as mentioned above , this would still be relevant with the use of MenuItems.
You should be aware that Content in Wpf can be set in 2 ways a Direct value like String , StaticResource , Binding (all are shorts of strings) , and a declarative value which you can describe a tree of elements as your content like iv'e shown below.
<ListView>
<ListView.Items>
<ListViewItem>
<ListViewItem.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Image Source="SomeSource.png" />
<TextBlock Text="Save" Grid.Column="1" />
</Grid>
</ListViewItem.Content>
</ListViewItem>
<ListViewItem Content="Save As" />
<ListViewItem Content="Open" />
<ListViewItem Content="Close" />
<ListViewItem Content="Info" />
<ListViewItem Content="Recent" />
<ListViewItem Content="New" />
</ListView.Items>
</ListView>

Related

How to remove the items left margin in a WPF ListView?

I have created a very simple ListView in WPF.
I would like to remove the left spacing that is present for each item (represented by the red arrows in my screenshot).
How can I do it?
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Company" />
</GridView>
</ListView.View>
<ListViewItem Content="A" />
<ListViewItem Content="B" />
<ListViewItem Content="C" />
</ListView>
Thank you
You have to set the ListViewItem.Margin to -6,0 to compensate the Margin from the GridView.
You can find that with SnoopWpf
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Company" />
</GridView>
</ListView.View>
<ListViewItem Content="A" Margin="-6,0" />
<ListViewItem Content="B" Margin="-6,0" />
<ListViewItem Content="C" Margin="-6,0" />
</ListView>

How to Group Listview items that are added through XAML?

I have a Listview in XAML with few Listview Items that are added through XAML only and my listview has no other Items source.I want to group the items into two group.My Listview code is something like this
<ListView Name="List">
<ListViewItem Content="Apple" />
<ListViewItem Content="Orange" />
<ListViewItem Content="Tomato" />
<ListViewItem Content="Potato" />
<ListView />
I want to group them into two sets .Is this possible.
I would say if you want to group items in ListView you will not go around the CollectionViewSource. It's easier to fill it in ViewModel, but if you haven't VM you can also create one in XAML.
<StackPanel>
<StackPanel.Resources>
<x:Array x:Key="arr" Type="{x:Type ListViewItem}">
<ListViewItem Content="Orange" Tag="Meal"/>
<ListViewItem Content="Apple" Tag="Meal"/>
<ListViewItem Content="Cat" Tag="Pets"/>
<ListViewItem Content="Dog" Tag="Pets"/>
<ListViewItem Content="Fish" Tag="Diverse"/>
<ListViewItem Content="Duck" Tag="Diverse"/>
</x:Array>
<CollectionViewSource x:Key="CVS" Source="{DynamicResource arr}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Tag" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</StackPanel.Resources>
<ListView ItemsSource="{Binding Source={StaticResource CVS}}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Label Content="{Binding Name}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</StackPanel>

How to change the background color of textboxes in listview in wpf

I have two listviews with textboxes in it. I want to change the background color of textbox if the textbox values are different after loading. How to do it?
Sample XAML is
<ListView Name="lstBase">
<ListView.View>
<DataTemplate>
<TextBox Name="txtBase" Text="{Binding BaseText}" Width="160" BorderThickness="0.5" BorderBrush="Black"/>
</DataTemplate>
</ListView.View>
</ListView>
<ListView Name="lstTarget">
<ListView.View>
<DataTemplate>
<TextBox Name="txtTarget" Text="{Binding TargetText}" Width="160" BorderThickness="0.5" BorderBrush="Black"/>
</DataTemplate>
</ListView.View>
</ListView>
<ListView>
<TextBox Background="black" Height="20" Width="90"/>
</ListView>

ListView header with clickable images

I've created the following header for a ListView in WPF but the MouseUp events from both of the images don't fire and I think it's because the header is handling the mouse. Can I disable the header? Or somehow make the images handle the mouse up?
XAML:
<ListView>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Colmun1" VerticalAlignment="Center"/>
<Image Source="Edit.png" Width="24" Height="24" MouseUp="Image_MouseUp"/>
<Image Source="Cancel.png" Width="24" Height="24" MouseUp="Image_MouseUp"/>
</StackPanel>
</GridViewColumn.Header>
</GridViewColumn>
</GridView>
</ListView.View>
<ListViewItem Content="Test"/>
<ListViewItem Content="Test"/>
<ListViewItem Content="Test"/>
<ListViewItem Content="Test"/>
<ListViewItem Content="Test"/>
</ListView>
Try Mouse.PreviewMouseUp Attached Event
UPDATE
You're right. It only works if you wrap the Image into a Button.
<Button PreviewMouseUp="Image_MouseUp">
<Image Source="Edit.png" Width="24" Height="24" />
</Button>
Maybe somebody else has a even better solution.

How do I get the ActualWidth of the GridViewColumn

I have the following XAML:
<GridView x:Key="myGridView">
<GridViewColumn CellTemplate="{StaticResource myTemplate}"/>
</GridView>
<DataTemplate x:Key="myTemplate">
<ContentPresenter Content="{Binding}"/>
</DateTemplate>
When I use it, I want the contentpresenter to have the same width as the GridViewColumn. So far, I've tried using the RelativeSource binding but I can't seem to get the GridViewColumn at all.
How do I get the ActualWidth from the parent GridViewColumn from the ContentPresenter in my DataTemplate?
The problem with GridView is that there is no notion of cells as visual elements in the generated control hierarchy. In order for your ContentPresenter to be wide as the column you should set the HorizontalContentAlignment property of the ListViewItem to Stretch. This way the ContentPresenter will stretch to the available width, which is the width of the column.
Here is a sample XAML which illustrates this:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="leftTemplate">
<ContentPresenter HorizontalAlignment="Left" Content="{Binding}"/>
</DataTemplate>
<DataTemplate x:Key="rightTemplate">
<ContentPresenter HorizontalAlignment="Right" Content="{Binding}"/>
</DataTemplate>
<GridView x:Key="myGridView">
<GridViewColumn Width="150" CellTemplate="{StaticResource rightTemplate}" Header="Right"/>
<GridViewColumn Width="150" CellTemplate="{StaticResource leftTemplate}" Header="Left"/>
</GridView>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</Grid.Resources>
<ListView Margin="10" View="{StaticResource myGridView}">
<ListViewItem Content="item 1"/>
<ListViewItem Content="item 2"/>
<ListViewItem Content="item 3"/>
<ListViewItem Content="item 4"/>
<ListViewItem Content="item 5"/>
</ListView>
</Grid>
</Page>

Resources