Hi please keep in mind I am new with WPF, I am using a WebService that returns a gridlist[] and it automatically populate my Gridview. here is the WebService:
C#:
WebService.Contacts Contact = new WebService.Contacts();
grdGetGroup.ItemsSource = Contact.GetGroups(Username, Password);
Here is my XAML:
<ListView x:Name="grdGetGroup"
Margin="560,34,128,48"
FontSize="13"
BorderBrush="#FFF01F1F"
Foreground="#FFF01F1F"
SelectedIndex="1"
FontFamily="/WPF Working Experimenet;component/Font/#B Nazanin">
<ListView.View>
<GridView>
<GridViewColumn x:Name="GridID"
Header="ID"
Width="50"
DisplayMemberBinding="{Binding GroupID}"
FrameworkElement.FlowDirection="RightToLeft" />
<GridViewColumn Header="Group Name"
Width="85
"
DisplayMemberBinding="{Binding GroupName}"
FrameworkElement.FlowDirection="RightToLeft" />
<GridViewColumn Header="Numbers"
Width="60"
DisplayMemberBinding="{Binding ContactCount}"
FrameworkElement.FlowDirection="RightToLeft" />
<GridViewColumn Header="Access"
Width="60"
DisplayMemberBinding="{Binding ShowToChild}"
FrameworkElement.FlowDirection="RightToLeft" />
<GridViewColumn Header="Description"
Width="150"
DisplayMemberBinding="{Binding GroupDescription}"
FrameworkElement.FlowDirection="RightToLeft" />
<GridViewColumn Header=""
Width="60"
FrameworkElement.FlowDirection="RightToLeft">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Button x:Name="btnChangeGroup"
Margin="5"
Content="Change"
Cursor="Hand"
Click="btnChangeGroup_Click">
<Button.Template>
<ControlTemplate TargetType="Button">
<TextBlock TextDecorations="Underline">
<ContentPresenter /></TextBlock>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Foreground"
Value="Black" />
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Foreground"
Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=""
Width="60"
FrameworkElement.FlowDirection="RightToLeft">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Button x:Name="btnRemoveGroup"
Margin="5"
Content="Remove"
Cursor="Hand"
Click="btnRemoveGroup_Click">
<Button.Template>
<ControlTemplate TargetType="Button">
<TextBlock TextDecorations="Underline">
<ContentPresenter /></TextBlock>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Foreground"
Value="Black" />
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Foreground"
Value="#FFF01F1F" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
If you have noticed, I have 2 button for every row in Gridview which is built automatically as well, now here is my question how to extract the Data in GridviewColumn x:Name"GridID"? and pass it to the same row's Button Click Event (Which has a method that takes the Data has an input)
Valkyry,
If I undestand/guess right, on every line you ve got a gridList item.
If I undestand/guess right each gridList has a GroupID property.
Correct me if am wrong.
In WPF, in a DataGrid, each line has a DataContext property that holds the data of the line :
void btnRemoveGroup_Click( Object sender, EventArgs args)
{
var fxElt = sender as FrameworkElement;
var lineData = fxElt.DataContext as gridlist;
int groupID = lineData .GroupID;
}
Tell me it works or not. Regards
Related
I have a ListView with GridView content as below. I just add the delete and edit button to the end of each row. So i don't need the header for those columns. I collopsed the headers for those columns, this removes the headers but the place of the header of the columns looks like white as below. How to set the style for those columns as the header bar style ?
<Style x:Key="sahin" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</Style>
<ListView Grid.Row="3" ItemsSource="{Binding deckList}" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Surname" Width="120" DisplayMemberBinding="{Binding SurName}" />
<GridViewColumn HeaderContainerStyle="{StaticResource sahin}" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Style="{StaticResource noBackgroundStyle}">
<Image Source="/KillCard;component/Resources/Images/delete.png" Width="16"></Image>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource sahin}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Style="{StaticResource noBackgroundStyle}">
<Image Source="/KillCard;component/Resources/Images/edit.png" Width="16"></Image>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The code snippet shown below helped me.
<Style x:Key="sahin" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
<Border x:Name="HeaderBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,1,0,1" Background="{TemplateBinding Background}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I set the background of my TextBlocks to different colors over the alternating colors. As you can see there is some gaps that are not colored in red:
Is there a way for me to remove those margins/spacing or stretch my TextBlock to completely fill those areas so the entire column region is colored?
Or is there a way to set the entire ListViewItem BG color?
Here is my XAML code:
<Window x:Class="ProfitTracker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ProfitTracker"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="450" Width="1200">
<Window.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#1e1e1e"></Setter>
<Setter Property="Foreground" Value="#ffffff"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#3c3c3c"></Setter>
<Setter Property="Foreground" Value="#ffffff"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Viewbox Stretch="Uniform">
<ListView Grid.Row="4" Grid.Column="0" Margin="0,0,0,0" ItemsSource="{Binding Coins}" Name="Tasks" Height="250" AlternationCount="2">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="" Width="25" DisplayMemberBinding="{Binding Path=Value.X}">
</GridViewColumn>
<GridViewColumn Header="" Width="25" DisplayMemberBinding="{Binding Path=Value.X}"></GridViewColumn>
<GridViewColumn Header="Symbol" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.Symbol}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Lowest" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.LowestDailyPriceDisplay}" Foreground="#ff0000" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Price" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.PriceDecimalDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Highest" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.HighestDailyPriceDisplay}" Foreground="#00ff00" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Unit Price" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.Balance.UnitPriceDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Amount" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.Balance.AvailableAmountDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Capital" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.Balance.TotalValueDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Profit" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.Balance.ProfitDisplay}" Background="{Binding Path=Value.Balance.ProfitPercentageColor}" Foreground="{Binding Path=Value.Balance.ProfitColor}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="PNL %" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.Balance.ProfitPercentageDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Day %" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.PriceChangeInPercentDailyDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Hour %" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.PriceChangeInPercentHourlyDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Min %" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.PriceChangeInPercentMinutelyDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Vol BTC/h" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.LastHourVolumeInBtcDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Net BTC/m" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value.LastMinuteVolumeInBtcDisplay}" TextAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Viewbox>
</Window>
I tried DataTriggers but I am unable to set the ListViewItem background color based on its column header, for example "Profit":
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Name}" Value="Profit">
<Setter Property="Background" Value="Red" />
</DataTrigger>
Is there a way to access the column header from a listviewitem reference?
The problem in your case is, that control template for the ListViewItem has a Border with Padding=2. The only possibility to get rid of it I see to replace the control template with own. So you can add following to your ListView :
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="Border" Padding="0" SnapsToDevicePixels="true" Background="{TemplateBinding Background}">
<GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
For more details see also ListView Styles and Templates
If you need you can also customize the VisualStateManager.
I have a ListView that contains a GridView. I am trying to change both the background of an item that is selected, and the background of an item that is hovered over. After some searching, I came across the control template in conjunction with triggers, but they're not firing.
Here is a screenshot of what I mean
<ListView x:Name="TestListView" Margin="0,0.333,0.333,0" Grid.Row="1" Grid.Column="1" SelectedIndex="0" MouseDoubleClick="Song_List_DoubleClick" SelectionMode="Single" Background="#FF181818" Foreground="White" BorderBrush="#FF181818">
<ListView.View>
<GridView>
<GridViewColumn Header="A" Width="120" DisplayMemberBinding="{Binding A}" />
<GridViewColumn Header="B" Width="50" DisplayMemberBinding="{Binding B}" />
<GridViewColumn Header="C" Width="150" DisplayMemberBinding="{Binding C}" />
<GridViewColumn Header="D" Width="150" DisplayMemberBinding="{Binding D}" />
</GridView>
</ListView.View>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#282828"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#333333" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView>
Any help is much appreciated. Thanks!
You set ControlTemplate but don't have any child control to display data. And the style you need to modify is ListViewItem, not ListBoxItem.
You can do like this:
<ListView x:Name="TestListView" Margin="0,0.333,0.333,0" Grid.Row="1" Grid.Column="1" SelectedIndex="0" SelectionMode="Single" Background="#FF181818" Foreground="White" BorderBrush="#FF181818">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="A" Width="120" DisplayMemberBinding="{Binding A}" />
<GridViewColumn Header="B" Width="50" DisplayMemberBinding="{Binding B}" />
<GridViewColumn Header="C" Width="150" DisplayMemberBinding="{Binding C}" />
<GridViewColumn Header="D" Width="150" DisplayMemberBinding="{Binding D}" />
</GridView>
</ListView.View>
</ListView>
I have array of class Person in ViewModel and I want to show their names in table. I have also column with checkboxes. This is my View part:
<ListView ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox
.........................
.... some logic here ....
......................./>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="140"
Header="Name"
DisplayMemberBinding="{Binding Path=Name}" />
</GridView>
</ListView.View>
</ListView>
How can I show/hide checkboxes column according to value of IsSelectionAllowed boolean variable.
Use style:
<Style x:Key="CheckBoxStyle" TargetType="{x:Type Control}">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelectionAllowed}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
Attach with:
<CheckBox Style="{StaticResource CheckBoxStyle}"
.........................
.... some logic here ....
......................./>
I have an expander containing a list view. The expander is initially disabled, and enabled sometime after the application is started.
Everything works great if before InitializeComponent() I populate the ListView's ItemsSource (some observable collection, created of course before InitializeComponent) with at least one item. The expander expands correctly, items show as they should. The only problem then is that new items that require more space to be rendered appear cropped - I assume this means the ListView isn't refreshing properly?
However, if I add items to the collection anytime after InitializeComponent(), the expander only expands a little bit (just a few pixels). When I force the ListView's Width to be some large value, the expander does expand, but items inserted into the list are rendered just with the background color, with no content...
I assume this has something to do with the ListView initialization, refresh or what have you. Code follows:
XAML:
<DockPanel Name="mainPanel">
<Expander DockPanel.Dock="Right" ExpandDirection="Right" IsExpanded="False" IsEnabled="{Binding ExpanderEnabled}">
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="Background" Value="#FFFF3333" />
<Style.Triggers>
<DataTrigger Binding="{Binding ExpanderEnabled}" Value="True">
<Setter Property="Background" Value="LightGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<Expander.Header>
<Grid Width="30" VerticalAlignment="Top" HorizontalAlignment="Left">
<Grid Width="300" Height="300" VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBlock Text="Expander Name" FontSize="12" FontWeight="Bold" Margin="22,170,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" RenderTransformOrigin="0,1">
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-90"/>
<TranslateTransform/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
</Grid>
</Expander.Header>
<ListView ItemsSource="{Binding Items}" BorderBrush="LightGreen" BorderThickness="3">
<ListView.Style>
<Style TargetType="ListView">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black"/>
</Style.Resources>
</Style>
</ListView.Style>
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn DisplayMemberBinding="{Binding Path=A, Converter={StaticResource aConverter}}" Width="Auto" />
<GridViewColumn DisplayMemberBinding="{Binding Path=B, Converter={StaticResource bConverter}}" Width="Auto" />
<GridViewColumn DisplayMemberBinding="{Binding Path=C, Converter={StaticResource cConverter}}" Width="Auto" />
<GridViewColumn DisplayMemberBinding="{Binding Path=D, StringFormat='\{0\} ds'}" Width="Auto" />
</GridView>
</ListView.View>
</ListView>
</Expander>
...