i have a list view like this .
<ListView x:Name="Source_List"
ItemsSource="{Binding Lines}"
IsSynchronizedWithCurrentItem="True"
SelectionChanged="Source_List_SelectionChanged">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Line"
Width="50"
DisplayMemberBinding="{Binding LineNumber}" />
<GridViewColumn Header="Start Time"
Width="100"
DisplayMemberBinding="{Binding StartTime , Converter={StaticResource LineTimeToString}}" />
<GridViewColumn Header="End Time"
Width="100"
DisplayMemberBinding="{Binding EndTime ,Converter={StaticResource LineTimeToString}}" />
<GridViewColumn Header="Text"
Width="500">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Context ,Mode=TwoWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Original Text"
DisplayMemberBinding="{Binding Context ,Mode=OneTime}" />
</GridView>
</ListView.View>
</ListView>
I want to access the text box inside the selected items as a textbox.(in code behind )
how can i do this?
i used this article by beth massi.
you can acces to textbox ui element (if your texblock is clicked)
TextBlock content = ((FrameworkElement)e.OriginalSource) as TextBlock;
else if your texblock is inside a grid use
Grid c = ((FrameworkElement)e.OriginalSource) as Grid;
and search for the grid(c) children.
Because you are using IsSynchronizedWithCurrentItem="True" you should be able to access the current item of your list 'Lines'. Your text box is bound to Context so I would assume in your view model you would be able to call 'Lines.CurrentItem.Context'
Related
I have one wpf project and in that I have one form and it it I have added one listview. I have loaded total 8 rows in it. And I have additionally added 2 more columns with text as Edit and Remove. Now what I need is on click of Edit and Remove click the respective row column details will come. Like if user click on Edit than the edit code should executes. Same thing with the Remove click also. I am attaching my wpf xaml file code here
<ListView x:Name="lstViewIrctcId" HorizontalAlignment="Left" Height="271" Margin="4,140,0,0" VerticalAlignment="Top" Width="777" >
<ListView.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
<EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn x:Name="Id" Header="ID" Width="135" DisplayMemberBinding="{Binding ID}" />
<GridViewColumn x:Name="Count" Header="TOTAL COUNT" Width="80" DisplayMemberBinding="{Binding COUNT}" />
<GridViewColumn x:Name="Name" Header="NAME" Width="80" DisplayMemberBinding="{Binding NAME}" />
<GridViewColumn x:Name="Address" Header="Address" Width="80" DisplayMemberBinding="{Binding ADDRESS}" />
<GridViewColumn x:Name="Status" Header="Status" Width="80" DisplayMemberBinding="{Binding STATUS}" />
<GridViewColumn x:Name="irEdit" Header="Edit" Width="60">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="Edit" MouseLeftButtonUp="Edit_btn_click">
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="irRemove" Header="Remove" Width="60">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="Edit" MouseLeftButtonUp="Delete_btn_click">
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I have tried adding the MouseLeftButtonUp in edit and remove and created its methods in .cs file. This detects which column clicked ie. Edit or Remove but I also need the respective row details in order to process the Edit and Remove operations.
in your list view xaml you should pass the listview items in a command like :
Command="{Binding GetSelectedItem}" CommandParameter="{Binding Path=PlacementTarget.SelectedItems, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
and in your code, after declaring the command as Relay command or delegateCommand you can call this function
public void YourCommandMethod(object obj)
{
try
{
//Get our list view
ListView listview = (ListView)obj;
string sNavigationPath = string.Empty;
//this is a work around to get selected item without clicking on list view, just the coordinates
var item = VisualTreeHelper.HitTest(listview, Mouse.GetPosition(listview)).VisualHit;
// find ListViewItem (or null)
while (item != null && !(item is ListViewItem))
item = VisualTreeHelper.GetParent(item);
if (item != null)
{
//Convert item to Listview
ListViewItem listviewItem = (ListViewItem)item;
//Get the data context wich hold the necessary info
var _dataContext = listviewItem.DataContext;
//Convert back to our customized listview
var _list = (ListViewItems)_dataContext;
// Do whatever you want with your elements
}
}
catch (Exception ex)
{
StaticElements.showErrorFlyout(ex.Message);
}
}
I have a ListView with DataTemplate defined in Resources. I would like to forward the content of the GridViewColumn to the DataTemplate so that the DataTemplate can be reused for multiple GridViewColumns. I have a property called Property1 which I want to bind to the GridViewColumn and then forwarded it to the DataTemplate where it will be displayed in the TextBlock. However, GridViewColumn doesn't have any Content property I could bind it to.
Here is a stripped down code:
<ListView>
<ListView.Resources>
<DataTemplate x:Key="PropertyTemplate>
<TextBlock Text"{Binding}" />
<DataTemplate>
</ListView.Resources>
<ListView.View>
<GridView>
<!-- No Content property in GridViewColumn -->
<GridViewColumn CellTemplate="{StaticResource PropertyTemplate}" Content={Binding Property1} />
<GridView>
</ListView.View>
</ListView>
How can I forward the the bound property from the GridViewColumn to the GridViewColumn's DataTemplate?
Update:
This should work:
<ListView ItemsSource="{Binding Items}">
<ListView.Resources>
<DataTemplate x:Key="PropertyTemplate">
<TextBlock Text="{Binding Property1}" />
</DataTemplate>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn CellTemplate="{StaticResource PropertyTemplate}" />
</GridView>
</ListView.View>
</ListView>
Previous answer (doesn't work):
Use DisplayMemberBinding property. You also probably want to set the header text for your column:
<GridView>
<GridViewColumn CellTemplate="{StaticResource PropertyTemplate}" DisplayMemberBinding={Binding Property1} Header="Some column"/>
</GridView>
You will also need to set ItemsSource on your ListView.
I've been messing around with checkboxes and listview in my wpf application. Selecting multiple items seems to work properly, but when I click on an item and not on its checkbox it selects the item and deselects all the others previously selected, how can I fix this ?
Here's my code:
<ListView Name="NewPlace_ActionsList" HorizontalAlignment="Left" Height="254" Margin="10,74,0,0" VerticalAlignment="Top" Width="389" >
<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="Codice" DisplayMemberBinding="{Binding Path=Id}" />
<GridViewColumn Header="Nome" DisplayMemberBinding="{Binding Path=Name}" />
<GridViewColumn Header="Descrizione" DisplayMemberBinding="{Binding Path=Desc}" />
</GridView>
</ListView.View>
</ListView>
What do you want to accomplish? The system behaves exactly to what you coded: the checkbox is selected only in the selected row. Because the Checkbox "follows" the IsSelected of the row (ListViewItem).
So far i have been working with listview without any problem. I have been able to pass multiple columns and fill them with images, text etc. I decided to do the same thing with a listbox but things there got complicated.
In a listview i do the following :
<DataTemplate x:Key="ImageTemplate" >
<Image x:Name="FavoriteImageList" Tag="{Binding tagger}" Width="12" Height="12" Source="{Binding ImageSource}" MouseLeftButtonDown="FavoriteImageList_MouseLeftButtonDown"/>
</DataTemplate>
<ListView x:Name="Citys">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
<GridViewColumn Width="90" Header="Image" CellTemplate="{StaticResource ImageTemplate}"/>
<GridViewColumn Width="178" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="178" Header="City" DisplayMemberBinding="{Binding City}"/>
<GridViewColumn Width="140" Header="Author" DisplayMemberBinding="{Binding Author}"/>
</GridView>
</ListView.View>
</ListView>
And i fill columns like this :
foreach(var fav in Favorites)
{
Citys = new List<ACity>()
{
new ACity() {tagger = Name ,ImageSource = ImageSource ,Name= Name, Author= Author, City = City}
};
ListBox.Items.Add(Citys);
}
If I do this in a listbox, it will fill with (Collection).
Is there a way to do this in a listbox, passing images buttons strings in a listbox row?
See the example from the MSDN ItemsControl.ItemTemplate
<ListBox Width="400" Margin="10"
ItemsSource="{Binding Source={StaticResource myTodoList}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
It should be pretty straight forward to match with your example.
I've following ListView Item (in a WPF Form):
<ListView Name="listViewTeam" ItemsSource="{Binding Path=TeamList}">
<ListView.View>
<GridView ColumnHeaderTemplate ="{StaticResource BlueHeader}">
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn Width="34" Header="Nr" DisplayMemberBinding="{Binding Path=TeamNr , Mode=OneWay}"/>
<GridViewColumn Header="Team" DisplayMemberBinding="{Binding Path=TeamName, Mode=OneWay}"/>
</GridView>
</ListView.View>
</ListView>
DataContext is a TeamViewModel, which contains
a) a List of Teams
b) a SelectedTeam Property (which is a Team-Object and contains Team.TeamName and Team.TeamNr )
Loading of the TeamViewModel.TeamListe into the ListView works fine (I get all Team-Objects from the list displayed in my ListView)
How can I set the TeamViewModel.SelectedTeam Property to the Row-Value, which is selected?
Thanks!
Cheers
Set the ListView's SelectedItem property to {Binding Path=SelectedTeam}