listview with checkbox get selected row index - c#

here is my problem , i have a listview with checkbox , and i want to get the index of the row which is selected
i want to get the index of the row to disable this row after validation
i try different method
CheckBox cbx = sender as CheckBox.tag;
if (cbx != null) {
var index = cbx.Tag.ToString();
}
(((ContentPresenter)((CheckBox)sender).TemplatedParent)).IsEnabled = false; with this i disable just the checkbox
CheckBox cbx = sender as CheckBox.tag;
int index = (int)(sender as CheckBox).Tag;

Use Below Code For One Row:
int index = YourListView.SelectedIndex;

Try below sample code.
Add a property as below
public int Selected
{
get { return _selected; }
set
{
_selected = value;
OnPropertyChanged(new PropertyChangedEventArgs("Selected"));
}
}
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
Inherit INotifyPropertyChanged into your cs file to get OnPropertyChanged
In the view bind the above property in listview
<Grid>
<ListView Margin="10" Name="lvUsers" SelectedIndex="{Binding Selected}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding ID}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
Try checking if the checkbox is disabled or not with this property. I hope this will works for you.

Related

find selected item in wpf xml

i have this xml code for show list view item in wpf :
<ListView FlowDirection="RightToLeft" Name="ListViewPost" HorizontalAlignment="Left" Height="504" Margin="1060,172,0,0" VerticalAlignment="Top" Width="304" Background="White" BorderBrush="Black">
<ListView.View >
<GridView>
<GridViewColumn Width="300" Header="عنوان" DisplayMemberBinding="{Binding Title}"/>
</GridView>
</ListView.View>
</ListView>
and using this code for show id in messsagebox :
private void listView1_MouseClick_1(object sender, RoutedEventArgs e)
{
int Id;
if (ListViewPost.SelectedIndex == -1) return;
Id = (int)ListViewPost.SelectedItems[0];
MessageBox.Show(Id.ToString());
}
now i put the break point in this function but it not enter in this . whats the problem ?
You could handle the SelectionChanged event:
private void ListViewPost_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
int Id;
if (ListViewPost.SelectedIndex == -1) return;
Id = (int)ListViewPost.SelectedItems[0];
MessageBox.Show(Id.ToString());
}
XAML:
<ListView FlowDirection="RightToLeft" Name="ListViewPost" HorizontalAlignment="Left" Height="504"
VerticalAlignment="Top" Width="304" Background="White" BorderBrush="Black"
SelectionChanged="ListViewPost_SelectionChanged_1">
<ListView.View >
<GridView>
<GridViewColumn Width="300" Header="عنوان" DisplayMemberBinding="{Binding Title}"/>
</GridView>
</ListView.View>
</ListView>
It will be raised whenever a new item is selected.

c# WPF Checkbox delete in Listview Grid

how do i delete specific row by select few checkbox and delete them same time?
and also update the XML file
im using sharp serialize
image
in the image you cant see how it should look like
Xaml:
<ListView Name="Trilogi" HorizontalAlignment="Left" Height="201" Margin="265,10,0,0" VerticalAlignment="Top" Width="240" SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="" Width="30">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="Chck" Checked="Chck_Checked" Command="{Binding Check}" Width="50" ></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="ID" Width="40" DisplayMemberBinding="{Binding Veh_Key}"/>
<GridViewColumn Header="Plate Number" Width="80" DisplayMemberBinding="{Binding Veh_Value}"/>
</GridView>
</ListView.View>
</ListView>
<Button Name="Del" Content="Delete" HorizontalAlignment="Left" Height="27" Margin="523,17,0,0" VerticalAlignment="Top" Width="102" Click="Button_Click"/>
the add is work fine
only the select items in the checkbox
cs:
private void Chck_Checked(object sender, RoutedEventArgs e)
{
foreach (var item in Trilogi.SelectedItem.ToString())
{
i
veh[item].Veh_Is_Checked = true;
MessageBox.Show(veh[item].Veh_Key.ToString());
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
foreach(var item in Trilogi.Items)
{
veh.Remove(item as Car);
}
}
Sorry, I can't comment. Don't have enough rep to do. But the answer i will give will assume you use are adding directly to the listview.
I think what you need to do is to create first a List of items when they are checked:
List<yourclass> selectedItems = new List<yourclass>();
Then on your xaml, you can add as tag the object you are using for binding:
<CheckBox Name="Chck" Checked="Chck_Checked" Command="{Binding Check}" Width="50" tag="{Binding}"></CheckBox>
and then what you can do in your check event is to add or remove the items, depending on the value of the checkbox say:
private void Chck_Checked(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
yourclass newVal = (yourclass)chk.Tag;
if (chk.IsChecked.HasValue && chk.IsChecked.Value)
{
selectedItems.Add(newVal);
}
else
{
selectedItems.Remove(newVal);
}
}
and now on your delete button code, you can loop on the list and remove them. And now you can use the list to then call your code for removing them on your xml as basis on what item to remove
private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (var item in selectedItems)
{
Trilogi.Items.Remove(item);
}
callMethodToRemoveItemsToXML();
selectedItems.Clear();
}

(WPF, C#, ListView) How can make event when user selected item

It's have to double click(click for select item=> click same item for event, it is almost like double click).
I want to make just one click event(when item select).
**Listview.xaml**
<ListView x:Name="listV1" HorizontalAlignment="Left" Height="263"
Margin="44,159,0,0" VerticalAlignment="Top" Width="283" BorderThickness="0" FontFamily="NanumSquareOTF" FontSize="18">
<ListView.View >
<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding TITLE}" Width="250" />
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown1" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
**Listview.xaml.cs**
private void ListViewItem_PreviewMouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
{
var item = sender as ListViewItem;
if (item != null && item.IsSelected)
{
uid_tmp = "";
DbData selectedItem = (DbData)listV1.SelectedItems[0];
db_tmp = selectedItem.Db;
Get_UIDDataAsync();
}
}
I solved use SelectionChanged
**list.xalm**
<ListView x:Name="listV1" HorizontalAlignment="Left"
Height="263" Margin="44,159,0,0" VerticalAlignment="Top"
Width="283" BorderThickness="0" FontFamily="NanumSquareOTF"
FontSize="18"
SelectionMode="Single"
SelectionChanged="listV1_SelectionChanged"
>
<ListView.View >
<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding TITLE}" Width="250" />
</GridView>
</ListView.View>
</ListView>
**list.xaml.cs**
private void listV1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = listV1.SelectedItem;
if (item != null)
{
uid_tmp = "";
DbData selectedItem = (DbData)item;
db_tmp = selectedItem.Db;
Get_UIDDataAsync();
}
}

How to get row data of each column if button is clicked inside listview

I am new to WPF. I have just bind one listview. Under my listview there is a button in each row.
When user clicks on that button, the data from that particular row should be fetched.
Below is my code
<Window x:Class="Searching.ImportedKeywords"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ImportedKeywords" Height="330.769" Width="1079.12">
<Grid Margin="-1094,0,2,24" RenderTransformOrigin="0.621,0.497">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<ListView Name="lvShowSearching" Height="247" VerticalAlignment="Top" RenderTransformOrigin="2.25,3.86" Margin="1100,10,0,-13">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" Width="70" DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="Last Name" Width="70" DisplayMemberBinding="{Binding LastName}" />
<GridViewColumn Header="With All Of The Words" Width="100" DisplayMemberBinding="{Binding WithAllOfTheWords}" />
<GridViewColumn Header="With The Exact Phrase" Width="100" DisplayMemberBinding="{Binding WithTheExactPhrase}" />
<GridViewColumn Header="With At Least One Of These Words" Width="100" DisplayMemberBinding="{Binding WithAtLeastOneOfTheseWords}" />
<GridViewColumn Header="Without The Word" Width="100" DisplayMemberBinding="{Binding WithoutTheWord}" />
<GridViewColumn Header="Country" Width="70" DisplayMemberBinding="{Binding CountryName}"/>
<GridViewColumn Header="State" Width="70" DisplayMemberBinding="{Binding StateName}" />
<GridViewColumn Header="City" Width="70" DisplayMemberBinding="{Binding CityName}" />
<GridViewColumn Header="Publication" Width="70" DisplayMemberBinding="{Binding PublicationName}" />
<GridViewColumn Header="Total Records" Width="70" DisplayMemberBinding="{Binding TotalRecords}" />
<GridViewColumn Header="Total Records Imported" Width="70" DisplayMemberBinding="{Binding TotalRecordsImported}" />
<GridViewColumn Header="Status" Width="70" DisplayMemberBinding="{Binding Status}" />
<GridViewColumn Header="Import" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding}" Click="Button_Click_1">Import</Button>
<!--<TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />-->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Address">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Margin="6,2,6,2">
<Button Content="Address"
Command="{Binding
Path=DataContext.RunCommand,
RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type ItemsControl}}}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
IEnumerable items = this.lvShowSearching.Items;
foreach (ClippingSearchKeyword data in items)
{
if (data != null)
{
}
}
}
private void OnRunCommand(object obj)
{
if (obj != null)
{
}
}
You can find your parent GridViewRow using below method
public static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}
And change ButtonClick event as below,You will get GridViewRow
private void Button_Click_1(object sender, RoutedEventArgs e)
{
GridViewRow grvRow= FindVisualParent<GridViewRow>(sender as UIElement);
}

Disabling button on a particular row in WPF gridview

This is my code for generating the gridview
User u1 = new User();
User u2 = new User();
for (int i = 0; i < files.Count; i++)
{
u1.Files = files[i];
items.Add(u1);
}
for (int i = 0; i < sharedFiles.Count; i++)
{
u2.Files += sharedFiles[i];
items.Add(u2);
}
gridview1.ItemsSource = items;
}
public class User
{
public string Files { get; set; }
}
This is my xaml page
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn Width="250" DisplayMemberBinding="{Binding Files}" ></GridViewColumn>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Download" Click="fileDownloadClick" CommandParameter="{Binding Files}"></Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Share" Click="fileShareClick" CommandParameter="{Binding Files}" ></Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
I would like to know if its possible to only allow my Share buttons disabled in my 2nd for loop (u2) while my Share buttons in (u1) is still enabled. Currently both are enabled. I am currently using this to retrieve values for particular role
private void fileShareClick(object sender, RoutedEventArgs e)
{
object share = ((Button)sender).CommandParameter;
}
Create a DataTrigger on the Button which you want. and bind that with property that represent the boolen type value. like IsShareAllowed.
Try some thing like this.
var procs = (from p in System.Diagnostics.Process.GetProcesses()
select new
{
Name = p.ProcessName,
IsShareAllowed = Convert.ToBoolean(r.Next(-1, 1))
}).ToList(); ;
list.ItemsSource = procs;
Xaml
<ListView Name="list" Grid.Row="4">
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn Width="250" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Download" Click="fileDownloadClick" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Share" Click="fileShareClick" >
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding IsShareAllowed}" Value="false">
<Setter Property="IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

Categories

Resources