wpf Listview with buttons - c#

I have a listview with buttons for each row, but when I bind the IsEnabled property to the SelectedIndex still keep enable all the buttons, I just need this one, and also I have another button that has to be enable depends on the property of the object in the listview.
e.g I have a class File that has a property bool HasLckFile, and I need that the download button keep disable until the user upload, but the binding is not working.
XAML:
<ListView x:Name="lv" Style="{StaticResource CustomListViewStyle}" Margin="470,0,0,0" SelectionMode="Multiple" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding Filename}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150">
<GridViewColumn DisplayMemberBinding="{Binding Filename}"/>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="download" VerticalAlignment="Center" Click="btnDownload">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=lv, Path=SelectedIndex}" Value="-1">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="upload" VerticalAlignment="Center" Click="btnUpload">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=lv, Path=SelectedIndex}" Value="-1">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

Related

Appearance of Textbox within a view column shall be identical to the appearance of list rows

My ListView contains a textbox per column to change data. Via Style my Listview rows are colored in different colors depending on the alternation index.
I want my textboxes within the listview to look like the entire line.
Actually its like this:
The lines should cover the textboxes.
The Code to color the ListView Rows:
<Style x:Key="DifAlternationColorsLV" TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#FFB1E4EF"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFD4EBFF"></Setter>
</Trigger>
</Style.Triggers>
</Style>
The code in the XAML of the View:
<Border Grid.Row="1" Grid.Column="0"
Margin="10,10,10,10"
BorderBrush="#FF474A57"
CornerRadius="10,10,10,10"
BorderThickness="2,2,2,2"
Width="300"
MaxHeight="200"
Background="White">
<StackPanel Margin="0,0,0,20" Orientation="Vertical">
<StackPanel Grid.Column="0" Grid.RowSpan="1"
Grid.Row="1"
VerticalAlignment="Top">
<Label HorizontalAlignment="Center" FontWeight="Bold">
Schichten
</Label>
<ListView x:Name="Shift" MinHeight="150" MaxHeight="150" MinWidth="250" HorizontalContentAlignment="Stretch" HorizontalAlignment="Center"
IsSynchronizedWithCurrentItem="True"
ItemContainerStyle="{StaticResource DifAlternationColorsLV}"
AlternationCount="2"
ItemsSource="{Binding UiShiftHModelList, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedLine, UpdateSourceTrigger=PropertyChanged}" d:ItemsSource="{d:SampleData ItemCount=3}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Bezeichnung" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="Bezeichnung" MinWidth="100"
Style="{StaticResource TBoxInListV}"
Text="{Binding Bezeichnung, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0">
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Tagmuster" Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<!---->
<TextBox x:Name="Tagmuster" MinWidth="80"
Background="{Binding ElementName=Shift}"
Style="{StaticResource TBoxInListV}"
Text="{Binding TagmusterID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0">
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Edit" Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="SaveShiftH"
Style="{StaticResource BtnListSave}"
Grid.Column="0">
</Button>
<Button x:Name="UpdateShiftH"
Style="{StaticResource BtnListUpdate}"
Grid.Column="0">
</Button>
<Button x:Name="DeleteShiftH"
Style="{StaticResource BtnListDelete}"
Grid.Column="1">
</Button>
<Button x:Name="ReopenShiftH"
Style="{StaticResource BtnListReopen}"
Grid.Column="0">
</Button>
</Grid>
</StackPanel>
<DataTemplate.Triggers>
<!--Visibility of the Buttons-->
<DataTrigger Binding="{Binding EditModus}" Value="0">
<Setter TargetName="SaveShiftH" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditModus}" Value="2">
<Setter TargetName="UpdateShiftH" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditModus}" Value="1">
<Setter TargetName="UpdateShiftH" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditModus}" Value="2">
<Setter TargetName="DeleteShiftH" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditModus}" Value="1">
<Setter TargetName="DeleteShiftH" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<!--<DataTrigger Binding="{Binding EditModus}" Value="0">
<Setter TargetName="Delete" Property="Visibility" Value="Collapsed"/>
</DataTrigger>-->
<DataTrigger Binding="{Binding EditModus}" Value="1">
<Setter TargetName="ReopenShiftH" Property="Visibility" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</StackPanel>
</Border>
If I change the Background Binding from the Textbox to:
Background="{Binding ElementName=Shift}"
The Line appears exactly I want it to but leads to a lot binding errors.
Any ideas?
You can make the background of the TextBox wholly or partially transparent; that will make the background of the row visible:
<TextBox x:Name="Bezeichnung" MinWidth="100" Background="#60FFFFFF"
Style="{StaticResource TBoxInListV}"
Text="{Binding Bezeichnung, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0">
</TextBox>

WPF ListView: Inline edition on button clicked

In an WPF ListView I have ListViewItems that consist of two columns, one column is a TextBlock and the another one contains two buttons (Edit and Delete) in the same column.
Also the first row of the WPF listView contains a button for adding new ListViewItems to the ListView. This first row is only for this purpose.
I have followed the example explained here. I have adapted it to my model but it is exactly the same.
Below the DataTemplate defined in the resources and later applied to the ListView:
<DataTemplate x:Key="FirstRowTemplate1">
<Grid Width="190">
<Button Content="Add" Command="{Binding Path=DataContext.AddCommand, ElementName=MyListView}"
Width="180"
HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="NextRowTemplate1">
<TextBlock Text="{Binding PathString}"/>
</DataTemplate>
<DataTemplate x:Key="NameColumnTemplate">
<ContentPresenter x:Name="ButtonPresenter"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
ContentTemplate="{StaticResource NextRowTemplate1}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Add}" Value="True">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource FirstRowTemplate1}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Add}" Value="False">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource NextRowTemplate1}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate x:Key="FirstRowTemplate2"/>
<DataTemplate x:Key="NextRowTemplate2">
<StackPanel Orientation="Horizontal">
<Button Content="Edit" Command="{Binding Path=DataContext.EditCommand, ElementName=MyListView}" CommandParameter="{Binding PathString}"/>
<Button Content="-" Command="{Binding Path=DataContext.DelCommand, ElementName=MyListView}" Margin="10 0 0 0" CommandParameter="{Binding PathString}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="AddDelItemTemplate">
<ContentPresenter x:Name="ButtonPresenter"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
ContentTemplate="{StaticResource NextRowTemplate2}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Add}" Value="True">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource FirstRowTemplate2}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Add}" Value="False">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource NextRowTemplate2}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Then they are applied to the ListView:
<ListView x:Name="MyListView"
ItemsSource="{Binding pathList}"
IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Paths" Width="350" CellTemplate="{StaticResource NameColumnTemplate}"/>
<GridViewColumn Width="70" Header="Edit/Delete" CellTemplate="{StaticResource AddDelItemTemplate}"/>
</GridView>
</ListView.View>
</ListView>
Now I am trying to perform an inline edit for the TextBlock once "Edit" button is clicked for this ListViewItem and exit edition mode once user validates it by pressing return key. If user press ESC key on edit mode, it should be exit edit mode and keep the value previous editing.How can I do this?

Listview double click problem in wpf caliburn micro

In my project I use list view and want to get the object from double click event,
I mean when user click on listview row, then from viewmodel I want to get the clicked object.
XAML:
cal:Message.Attach="[Event MouseDoubleClick] = [Action RowDoubleClick($dataContext)]"
ViewModel
public void RowDoubleClick(object pm)
{
}
but in object pm in ViewModel I get all off the listview object, I need a single object.
Can anyone help me regarding this issue.
XAML
<ListView AlternationCount="2" HorizontalAlignment="Center" cal:Message.Attach="[Event MouseDoubleClick] = [Action RowDoubleClick($dataContext)]"
Name="lvProcessList" SelectionChanged="lvProcessList_SelectionChanged"
ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True"
VerticalAlignment="Top" Width="779" Grid.ColumnSpan="8" IsSynchronizedWithCurrentItem="True" Height="391" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}" >
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="0,0,1,1" />
<Setter Property="Height" Value="30" />
<Setter Property="IsSelected" Value="{Binding chkWspSelect}"/>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="LightBlue" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGray" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="SteelBlue"/>
</Trigger>
</Style.Triggers>
<!--<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />-->
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<DataGrid AutoGenerateColumns="True" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="chkWspSelectAll" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="chkWspSelectAll_Checked" Unchecked="chkWspSelectAll_Unchecked" IsThreeState="False"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<GridViewColumn.CellTemplate >
<DataTemplate>
<CheckBox Name="chkWspSelect" HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="chkWspSelect_Checked" Unchecked="chkWspSelect_Unchecked" IsThreeState="False"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="200" Header="Process name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" Text="{Binding ProcessName}" TextAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="120" Header="Ongoing">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Ongoing}" TextAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="110" Header="Un-Strated">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Un-Strated}" TextAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="110" Header="Completed">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Completed}" TextAlignment="Center"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="200" Header="Action" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<WrapPanel >
<Button Width="50" Height="25" Margin="30,0,0,0" x:Name="btnDelete" Click="BtnDelete_Click">
<StackPanel Orientation="Horizontal" >
<Image Source="/Pipe_Flushing;component/Image/Trash.png" Height="20"/>
</StackPanel>
</Button>
<Button x:Name="btnHistory" Width="50" Height="25" Margin="10,0,0,0" Click="BtnHistory_Click">
<StackPanel>
<Image Source="/Pipe_Flushing;component/Image/History.png" Height="20"/>
</StackPanel>
</Button>
<Button x:Name="btnHistory1" Width="50" Height="25" Margin="10,0,0,0" Content="test" cal:Message.Attach="[Event Click] = [Action Edit($dataContext)]">
</Button>
</WrapPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

Style not taken in account on GridViewColumn ToolTip

So I have the following problem :
In my ListView, I want to add ToolTips to specific GridViewColumns.
Sometimes these ToolTips are empty, and I need to hide them.
When I have a ToolTip on a ListView Line, I encounter no problem doing the following in my App.xaml file:
<Style TargetType="ToolTip">
<Style.Triggers>
<Trigger Property="Content" Value="{x:Static sys:String.Empty}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="Content" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
But in the case a ToolTip is applied to only one column of my ListView, my XAML is the following :
<GridViewColumn Header="{x:Static p:Resources.Name}" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Name}" Tag="{Binding Name}"
MouseMove="mouseOverNameRepere">
<TextBlock.ToolTip>
<StackPanel>
<Grid>
<TextBlock Grid.Column="0"
Text="{Binding Path=ToolTipModifications}"
TextAlignment="Left" HorizontalAlignment="Left"/>
</Grid>
</StackPanel>
</TextBlock.ToolTip>
</TextBlock>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
How can I hide the ToolTip when it is empty? The code in my App.xaml is not working.
Also tried to do it in code behind :
TextBlock item = (TextBlock)sender;
ToolTip toolTip = (ToolTip)item.ToolTip;
But second line gives me an exception as item.ToolTip is a StackPanel object and cannot be converted?
In fact I calculate the ToolTip content only when I enter the TextBox element, so I thought I would then check and apply toolTip.Visibility at this moment, but I couldn't.
Your Style should work if you set the ToolTip property of the TextBlock like this:
<TextBlock Text="{Binding Path=Name}" Tag="{Binding Name}" MouseMove="mouseOverNameRepere"
ToolTip="{Binding Path=ToolTipModifications}" />
Use Rectangle instead of Tooltip
<GridViewColumn Header="{x:Static p:Resources.Name}" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Name}" Tag="{Binding Name}"/>
<Rectangle Fill="Transparent" ToolTipService.ToolTip="{Binding Path=ToolTipModifications}" MouseEnter="UIElement_OnMouseEnter"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Then check if ToolTipModifications is empty in MouseEnter event
private void UIElement_OnMouseEnter(object sender, MouseEventArgs e)
{
if (sender is Rectangle rectangle)
{
if (string.IsNullOrEmpty(rectangle.ToolTip.ToString()))
{
rectangle.Visibility = Visibility.Collapsed;
}
}
}
Please try this code:
<DataGridTextColumn Width="200" Binding="{Binding Name}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextBlockDefaultStyle}">
<Setter Property="ToolTip" Value="{Binding Name}">
<Setter Property="ToolTipService.ShowDuration" Value="6000">
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

DataTrigger is not firing

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:
<Grid>
<Grid.Resources>
<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>
</Grid.Resources>
<ListView ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Style="{StaticResource CheckBoxStyle}"
.........................
.... some logic here ....
......................./>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="140"
Header="Number"
DisplayMemberBinding="{Binding Path=Name}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
I want to show/hide checkboxes according to value of IsSelectionAllowed boolean variable. Why DataTrigger is not firing?
Assuming IsSelectionAllowed is a property on the view model set on the data context, you will need a relative source binding - hopefully this is correct without any testing:
<DataTrigger Binding="{Binding Path=IsSelectionAllowed, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
The binding of IsSelectionAllowed in your code is to the Person type.

Categories