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.
Related
I have a list view, of which the first column is checkboxes to enable/disable the item which represents said row. The problem is, when I change the checkbox, it does not select the row. Thus, when inside my checkBox checked/unchecked event I do listView.SelectedItem, it is null and crashes. Is there any way to fix this behavior so the listView check will select the item? Or, if there is an equivalent CheckedItems property in WPF like there is in WinForms?
XAML:
<Grid>
<ListView x:Name="listView1" HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="375">
<ListView.View>
<GridView>
<GridViewColumn x:Name="StatusHeader" Header="Status" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="5, 0" IsChecked="{Binding loaded}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="NameHeader" Header="Name" Width="130" DisplayMemberBinding="{Binding name}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
C#:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
module v = (module)listView1.SelectedItem; // crash
_client.AddClientModule(v.path);
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
module v = (module)listView1.SelectedItem; // crash
_client.RemoveClientModule(v.name);
}
You can use the DataContext to access the ViewModel and manually select it (if it is desired).
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
var cb = (CheckBox)sender;
var v = (module)cb.DataContext;
_client.AddClientModule(v.path);
// Select manually
listView1.SelectedItem = v;
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
var cb = (CheckBox)sender;
var v = (module)cb.DataContext;
_client.RemoveClientModule(v.path);
// Select manually
listView1.SelectedItem = v;
}
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();
}
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();
}
}
I am making a matching item user control.
On the left I have a Label that is bound to the value the user is trying to match.
On the right I have a ListVIew populated with possible matches for the value on the left.
After the user selects the match in the list I want to draw a line from the label to the selected ListView Item that will also update when the list is scrolled.
I don't know how to get positions and draw the line. Any ideas?
<Canvas Name="ConnectionCanvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" Background="Red">
<Grid Name="ConnectionGrid" Width="{Binding ActualWidth, ElementName=ConnectionCanvas}"
Height="{Binding ActualHeight, ElementName=ConnectionCanvas}">
<Label Name="SourceLabel" HorizontalAlignment="Left" VerticalAlignment="Center" Content="{Binding ValueToMatch}"/>
<ListView Name="DestinationList" Margin="5" Width="300" SelectionMode="Single"
HorizontalAlignment="Right" VerticalAlignment="Stretch"
ItemsSource="{Binding PossibleMatchLookupCollection}"
SelectedValuePath="Identifier" SelectedValue="{Binding Path=SelectedMatch}"
IsSynchronizedWithCurrentItem="True"
userControls:GridViewSort.AutoSort="True"
userControls:GridViewSort.SortGlyphAscending="pack://application:,,,/Divestco.Windows.Resources;component/PNGs/General/Small/UpArrow.png"
userControls:GridViewSort.SortGlyphDescending="pack://application:,,,/Divestco.Windows.Resources;component/PNGs/General/Small/DownArrow.png">
<i:Interaction.Behaviors>
<behaviors:ListViewGridViewColumnAutoWidthBehavior/>
</i:Interaction.Behaviors>
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridView.Columns>
<GridViewColumn Header="{Binding Path=FirstColumnDisplayName}" DisplayMemberBinding="{Binding Path=FirstColumnValue}" userControls:GridViewSort.PropertyName="FirstColumnValue" />
<GridViewColumn Header="{Binding Path=SecondColumnDisplayName}" DisplayMemberBinding="{Binding Path=SecondColumnValue}" userControls:GridViewSort.PropertyName="SecondColumnValue" />
<GridViewColumn Header="{Binding Path=ThirdColumnDisplayName}" DisplayMemberBinding="{Binding Path=ThirdColumnValue}" userControls:GridViewSort.PropertyName="ThirdColumnValue" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<Line Name="ConnectorLine" Stroke="Black" />
</Grid>
</Canvas>
Code behind:
private void HandleSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Point relativeLefttPoint = SourceLabel.TransformToAncestor(ConnectionGrid)
.Transform(new Point(0, 0));
UIElement selectedContainer = (UIElement)DestinationList.ItemContainerGenerator.ContainerFromIndex(DestinationList.SelectedIndex);
Point relativeRightPoint = default(Point);
if (selectedContainer != null)
{
relativeRightPoint =
selectedContainer.TranslatePoint(new Point(selectedContainer.DesiredSize.Width, 0.0), ConnectionGrid);
}
ConnectorLine.X1 = relativeLefttPoint.X;
ConnectorLine.X2 = relativeLefttPoint.Y;
ConnectorLine.Y1 = relativeRightPoint.X;
ConnectorLine.Y2 = relativeRightPoint.Y;
}
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.