ListView header with clickable images - wpf

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.

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 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>

WPF ListView: items with and without icon

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>

Buttons in ListBox

I'm doing a list in my project, that needs a button for each item.
How can I get those buttons in ListBox, like in the page "Last calls" of my windows phone?
Thanks.
FunksMaName answer is very much correct except a Small change.....
<ListBox Height="360"
HorizontalAlignment="Left"
Margin="22,23,0,0"
Name="UserDetailsListBox"
VerticalAlignment="Top"
Width="413"> <ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button.Template>
<ControlTemplate>
<Image Source="/Assets/Images/MyImage.png" />
</ControlTemplate>
</Button.Template>
<TextBlock x:Name="txtOverViewHeader1"
Text="OverView"
Foreground="Yellow"
Width="600"
FontSize="28"
Margin="10,0,0,0"
Height="65">
</TextBlock>
</StackPanel>
</DataTemplate></ListBox.ItemTemplate></ListBox>
i just moved image inside button's template instead of content ... That is more accurate..
We may add button for each item of the Listbox like this.
<ListBox Height="360"
HorizontalAlignment="Left"
Margin="22,23,0,0"
Name="UserDetailsListBox"
VerticalAlignment="Top"
Width="413">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical"
>
<StackPanel Orientation="Horizontal">
<Button Width="150" Height="50" x:Name="Btn1" Content="Button1" Margin="0,-20,0,0"/>
<TextBlock x:Name="txtOverViewHeader1"
Text="OverView"
Foreground="Yellow"
Width="600"
FontSize="28"
Margin="10,0,0,0"
Height="65">
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Width="150" Height="50" x:Name="Btn2" Content="Button2" Margin="0,-20,0,0"/>
<TextBlock x:Name="txtOverViewHeader2"
Text="OverView"
Foreground="Yellow"
Width="600"
FontSize="28"
Margin="10,0,0,0"
Height="65">
</TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Hope it will give you the desired answer
The easiest way to get round buttons is to user Coding4Fun. It is free to use and very simple to install.
You can follow these instructions to use it in your application.
As for the icons, you can either search around for some free packs or you can see if Metro Studio 2 has the icon you need. That tool is also free to use.
Use a circular image, and set the Tap event, or wrap your image around a Button template if you need a buttons specific behavior
<ListBox Height="360"
HorizontalAlignment="Left"
Margin="22,23,0,0"
Name="UserDetailsListBox"
VerticalAlignment="Top"
Width="413">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Template="x:Null" Tap="">
<Image Source="/Assets/Images/MyImage.png" />
</Button>
<TextBlock x:Name="txtOverViewHeader1"
Text="OverView"
Foreground="Yellow"
Width="600"
FontSize="28"
Margin="10,0,0,0"
Height="65">
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

sort a GridView when a column header is clicked

this is my wpf file i want sort a GridView when a column header is clicked
i try this make <GridView AllowsColumnReorder="true"> but not working
sorry for my bad English
<ListView Name="deviceListBox"
Width="630"
Height="282"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ItemsSource="{Binding Items}"
SelectionChanged="deviceListBox_SelectionChanged"
SelectionMode="Single">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<Label Width="15"
Height="25"
Margin="10,0,0,0"
HorizontalAlignment="center"
VerticalAlignment="Center" />
</DataTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<controls:PresenceIndicator Width="35"
Height="30"
Margin="7,0,0,0"
HorizontalAlignment="center"
VerticalAlignment="Center"
PhotoDisplayMode="Large"
SingleClickAction="ShowContactDetails"
Source="{Binding Path=SipURI}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<Label Width="95"
Height="25"
Margin="10,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Content="Username"
Foreground="Black" />
</DataTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<Label Height="30"
Margin="7,0,0,0"
HorizontalAlignment="left"
VerticalAlignment="Center"
Content="{Binding Path=Username}"
Foreground="Black" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
I think ListView doest not support sorting by default. Yo need to create attached dependency property to handle the sorting.
refer - Automatically sort a GridView when a column header is clicked

Resources