I am created a listview and add combobox and button control into listview and a button to remove items in this view.I need to remove Listview item when clicking the Remove button inside the listview without clicking the row.
<ListView Name="listView">
<ListView.View>
<GridView>
<GridViewColumn Header="SlNo" Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtSlno" Width="35" Text="{Binding slno}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Bar Code" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cmbBarcode" IsEditable="True" Width="110" Text="{Binding barcode}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Item Name" Width="250">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="txtitemname" IsEditable="True" Width="240" Text="{Binding itemname}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Qty" Width="65">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtqty" Width="65" Text="{Binding qty}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Unit" Width="65">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtunit" Width="60" Text="{Binding unit}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Rate" Width="90">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtrate" Width="85" Text="{Binding rate}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Disc %" Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtdiscp" Width="75" Text="{Binding discp}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Disc" Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtdisc" Width="75" Text="{Binding disc}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Net Amount" Width="130">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtnetamount" Width="125" Text="{Binding netamount}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="MRP" Width="130">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="txtmrp" Width="125" Text="{Binding mrp}" PreviewKeyDown="txtmrp_PreviewKeyDown"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Remove" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnRemove" Content="Remove" Width="60" BorderThickness="0" CommandParameter="{Binding}" HorizontalContentAlignment="Right" Cursor="Hand" Foreground="Blue" Click="btnEdit_Click"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border CornerRadius="2" SnapsToDevicePixels="True"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<Border Name="InnerBorder" CornerRadius="1" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition MaxHeight="11" />
<RowDefinition />
</Grid.RowDefinitions>
<Rectangle Name="UpperHighlight" Visibility="Collapsed" Fill="#75FFFFFF" />
<GridViewRowPresenter Grid.RowSpan="2"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
The c# code is given below
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
listView.Items.RemoveAt(listView.SelectedIndex);
}
It working properly when first click on listview item and then click the remove button.I need to move item with one click on Remove button
If you are using binding this is how you get the DataContext to remove the row from the source
Button btn = (Button)sender;
SearchItem srchItem = (SearchItem)btn.DataContext;
I used an ICommand instead of button click events since I wanted to put the Command in the parent viewmodel instead of the code-behind, but I'll share anyways since it works for me and could be helpful for you.
Instead of Click="btnEdit_Click", I have Command="{Binding ParentViewModel.btnEditCommand". (I also have CommandParameter="{Binding}" as you do.)
Then in the ParentViewModel, called "MyViewModel":
public ObservableCollection<ListItemsViewModel> ListItems
{
get;
set;
}
public MyViewModel()
{
btnEditCommand = new RelayCommand<object>(btnEditCommand_Do, btnEditCommand_Can);
}
public ICommand btnEditCommand
{
get;
set;
}
public bool btnEditCommand_Can( object param )
{
return true;
}
public void btnEditCommand_Do( object param )
{
ListItems.Remove( param as ListItemsViewModel );
}
and finally, the ListItemsViewModel referenced above inherits from the same parent as MyViewModel inherits from.
Hope this helps.
// Try This Code
String txt = ((TextView)view).getText().toString();
adapter.remove(txt);
Related
I am working on wpf and using an ItemsControl to represent a list of class on the UI.
My code is like this:
<ItemsControl ItemsSource="{Binding Path=UsecaseListItems}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<CheckBox IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,IsAsync=True}"
Margin="2,5"/>
<Button Content="{Binding UsecaseName}"
Margin="2,5"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Now I wand to add a header to both the columns (Select all and UseName) at the top of the list. How can I do it??
Below is the solution (Not a good one but I supposed worse design are also possible) and please change bindings in your code:
XAML:
<ItemsControl ItemsSource="{Binding Path=list}" >
<ItemsControl.Template>
<ControlTemplate>
<StackPanel Orientation="Vertical">
<Border BorderThickness="5" BorderBrush="DarkGray">
<StackPanel Orientation="Horizontal">
<Button Content="Select All" Width="80" HorizontalAlignment="Stretch"/>
<Button Content="User name" Width="80" HorizontalAlignment="Stretch"/>
</StackPanel>
</Border>
<Border BorderThickness="5" BorderBrush="LightGray">
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=ItemsSource}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<CheckBox IsChecked="{Binding Path=DeviceState,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,IsAsync=True}"
HorizontalAlignment="Stretch" Width="100" Margin="2,5"/>
<Button Content="{Binding Name}" HorizontalAlignment="Stretch" Width="100" Margin="2,5"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
</StackPanel>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
Output
Why do you want buttons as header? can't you just put buttons outside the ItemsControl?
It is preferable to go with ListView control instead of ItemsControl for what you need.
Here is the sample. .
<Grid>
<Grid.Resources>
<DataTemplate x:Key="checkboxHeaderTemplate">
<CheckBox Checked="CheckBox_Checked"> //add command for MVVM
</CheckBox>
</DataTemplate>
<DataTemplate x:Key="CheckBoxCell">
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" >
</CheckBox>
</DataTemplate>
<DataTemplate x:Key="ButtonCell">
<Button Content="{Binding Path=UsecaseName, Mode=TwoWay}" >
</Button>
</DataTemplate>
</Grid.Resources>
<ListView ItemsSource="{Binding Path=UsecaseListItems}" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}"
CellTemplate="{StaticResource CheckBoxCell}" Width="auto">
</GridViewColumn>
<GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}"
CellTemplate="{StaticResource ButtonCell}" Width="auto">
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
Hope that helps.
I have a ListView that uses data templates to display specific controls within the cells:
<ListView Grid.Row="1" ItemsSource="{Binding Ratings}" SelectedItem="{Binding SelectedRating}" Margin="5" MinHeight="50" SelectionMode="Single" ScrollViewer.CanContentScroll="True"
local:GridViewSort.Enable="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" local:GridViewSort.PropertyName="RatingDate">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding RatingDate}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Rating ID" local:GridViewSort.PropertyName="RatingID">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding RatingID}" Width="35" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" local:GridViewSort.PropertyName="RatingName">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding RatingName}" Width="35" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Age" local:GridViewSort.PropertyName="RatingAge">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding RatingAge}" Width="35" />
<TextBlock Text=" yrs"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I also have a delete button to remove the selected row.
My user has a habit of clicking into to the edit and date controls (which doesn't change the selected row) and then hitting delete to remove the row he has clicked on. Unfortunately, this deletes the wrong row.
How can I change the row selection whenever the user clicks into the controls?
You can use PreviewMouseUp event on a ListViewItem:
<ListView Grid.Row="1" ItemsSource="{Binding Ratings}" SelectedItem="{Binding SelectedRating}" Margin="5" MinHeight="50" SelectionMode="Single" ScrollViewer.CanContentScroll="True"
local:GridViewSort.Enable="True">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="PreviewMouseUp" Handler="ListViewItem_PreviewMouseUp" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
...
Code behind:
private void ListViewItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
var listViewItem = sender as ListViewItem;
if (listViewItem != null)
{
listViewItem.IsSelected = true;
}
}
I am trying to create a DataTemplate that can be shared for all columns of a GridView, which has it's columns created dynamically (through code-behind).
I would like to create the DataTemplate as a resource in XAML instead of entirely in code-behind, but I can't figure out how to get the bindings to work properly.
The following is the closest I could come up with (but does not work):
<DataTemplate x:Key="ListViewCellTemplate">
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type GridViewColumn}}}" />
</DataTemplate>
This template is assigned as the CellTemplate of each column as follows:
BindableDataTable table = this.DataContext as BindableDataTable;
foreach (BindableDataColumn c in table.Columns)
{
GridViewColumn col = new GridViewColumn();
col.Header = c.ColumnName;
col.CellTemplate = this.FindResource("ListViewCellTemplate") as DataTemplate;
v.Columns.Add(col);
}
Answer:
Set a DataTemplate in your resources
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate x:Key="GridViewCellTemplateStyle">
<TextBlock Text="{Binding}">
<TextBlock.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DataContext.CommandDoubleClick, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
Create your grid view and make the columns inherit this datatemplate
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" Header="Column1" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
<GridViewColumn Width="Auto" Header="Column2" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
<GridViewColumn Width="Auto" Header="Column3" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
<GridViewColumn Width="Auto" Header="Column4" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
</GridView>
</ListView.View>
</ListView>
This exmaple shows you how to create a double clickable row in a GridViewColumn.
Just change the DataTemplate as you see fit
I'm having the exact same problem.
I want to be able to apply the data template to different columns but have each column bound to a separate data field.
<ListView ItemsSource="{Binding}" Name="listViewIMS" Grid.Row="1" Margin="0,0,0,4" FontSize="11" AlternationCount="2" SelectionMode="Extended">
<ListView.Resources>
<DataTemplate x:Key="Templ">
<TextBlock TextAlignment="Left" Text="{Binding}"/>
</DataTemplate>
<DataTemplate x:Key="Tempr">
<TextBlock Width="78" Margin="-6,0" TextAlignment="Right" Text="{Binding}" />
</DataTemplate>
<Style x:Key="HeaderStyleRight" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style x:Key="HeaderStyleLeft" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView x:Name="gridViewInvoices">
<GridViewColumn Width="80" DisplayMemberBinding="{Binding Document}">
<GridViewColumnHeader Tag="docNo" Content="Document" />
</GridViewColumn>
<GridViewColumn Width="220" DisplayMemberBinding="{Binding Customer}">
<GridViewColumnHeader Tag="customer" Content="Customer"/>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding inv_date, StringFormat='dd/MM/yy'}" >
<GridViewColumnHeader Tag="date" Content="Date" />
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding inv_l_catalogue}" >
<GridViewColumnHeader Tag="catalogue" Content="Item" />
</GridViewColumn>
<GridViewColumn Width="60" DisplayMemberBinding="{Binding inv_l_qty}" >
<GridViewColumnHeader Tag="qty" Content="Qty" />
</GridViewColumn>
<GridViewColumn Width="80" DisplayMemberBinding="{Binding inv_l_price, StringFormat='0.00' }" >
<GridViewColumnHeader Tag="unitPrice" Content="Price" />
</GridViewColumn>
<GridViewColumn Width="50" DisplayMemberBinding="{Binding inv_l_per}" >
<GridViewColumnHeader Tag="per" Content="Per" />
</GridViewColumn>
<GridViewColumn Header="Goods" CellTemplate ="{StaticResource Tempr}" HeaderContainerStyle="{StaticResource HeaderStyleRight}" Width="80" >
<!--GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="78" Margin="-6,0" TextAlignment="Right" Text="{Binding inv_l_lgoods2, StringFormat='0.00'}" />
</DataTemplate>
</GridViewColumn.CellTemplate>-->
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
#jonathan-beresford
This question is old... But, for future reference:
You can binding on the template
<DataTemplate x:Key="Templ">
<TextBlock TextAlignment="Left" Text="{Binding inv_l_catalogue}"/>
</DataTemplate>
I am new to WPF and entity framework. I have ran into the the following problem while working on an application. In my application I am binding my data using collection view source from entity framework. one of my database table has a column named isNumeric datatype Boolean. if its is true then my grid view in WPF window should show the text "Numeric" and "String" if false. For this requirement I cant directly bind the my linq query result with the grid view or any control in the UI. Any thoughts on how could I solve this.
Here some of my codes
MainWindow.XAML.cs code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private PartNumbersEntities partNumberContext = new PartNumbersEntities();
private PartNumbersCollection partNumberData;
//private PartClassesCollection partClassData;
private CollectionViewSource MasterViewSource;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//string classFilter = classNameTextBox.Text;
//if (classFilter.Length == 0)
classFilter = "Dis";
//MessageBox.Show(classFilter);
var result = partNumberContext.PartNumbers.Where(p => p.PartClass.chrPCName.Contains(classFilter)).Select(p => p);
this.partNumberData = new PartNumbersCollection(result, partNumberContext);
this.MasterViewSource = (CollectionViewSource)this.FindResource("MasterView");
this.MasterViewSource.Source = this.partNumberData;
}
}
My partNumber Collection
class PartNumbersCollection : ObservableCollection<PartNumber>
{
private PartNumbersEntities _context;
public PartNumbersEntities Context
{
get { return _context; }
}
public PartNumbersCollection(IEnumerable<PartNumber> partNumbers, PartNumbersEntities context)
: base(partNumbers)
{
_context = context;
}
}
Xaml Code
<Window x:Class="Engenious.PartNumbersUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="454" Width="1033" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="MasterView" />
<CollectionViewSource x:Key="PartProperties"
Source="{Binding Source={StaticResource MasterView},
Path='PartProperties'}"/>
<!--<CollectionViewSource x:Key="PartNumberView"
Source="{Binding Source={StaticResource MasterView},
Path='PartNumbers'}"/>-->
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource MasterView}}">
<Grid.RowDefinitions>
<RowDefinition Height="42" />
<RowDefinition Height="310" />
<RowDefinition Height="42" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Name="Grid0">
<StackPanel Name="StackPanel1" Orientation="Horizontal">
<Label Content="Class Filter" Height="28" Name="label1" Margin="3" />
<TextBox Height="28" Name="classNameTextBox" Width="120" Margin="3"/>
<Button Content="Apply" Height="28" Name="applyButton" Width="80" Margin="3"/>
</StackPanel>
</Grid>
<ListView Grid.Row="1" Name="ListView1" VerticalContentAlignment="Top"
VerticalAlignment="Top" HorizontalAlignment="Left" Height="310"
Width="320"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding }">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Part Class Name" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=PartClass.chrPCName}" Margin="-6,0,-6,0"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Part Number" Width="130">
<GridViewColumn.CellTemplate>
<DataTemplate>
<!--<Label Content="{Binding Source={StaticResource PartNumberView},Path=chrPNPartNumber}" Margin="-6,0,-6,0"/>-->
<Label Content="{Binding Path=chrPNPartNumber}" Margin="-6,0,-6,0"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ListView Grid.Row="1" Height="310" HorizontalAlignment="Left" Margin="350,0,0,0" Name="listView2"
VerticalAlignment="Top" Width="375"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource PartProperties}}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=ConfigurationProperty.chrCPProperty}" Margin="-6,0,-6,0"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Datatype" Width="130">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=ConfigurationProperty.bitCPIsNumeric}" Margin="-6,0,-6,0"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel Name="StackPanel4" Orientation="Horizontal" Grid.Row="2">
<Button Height="25" Name="btnAddDetail" Width="82" Margin="3">Save</Button>
<Button Height="26" Name="btnDeleteDetail" Width="83" Margin="3">Delete</Button>
</StackPanel>
</Grid>
Here is a Screen shot
I have used data Trigger to solve this problem..
Here is my XAML for it.
<GridViewColumn Header="Datatype" Width="130">
<GridViewColumn.CellTemplate>
<DataTemplate >
<Label>
<Label.Resources>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path = 'ConfigurationProperty.bitCPIsNumeric'}" Value="True">
<Setter Property="Content" Value="Numeric" />
</DataTrigger>
<DataTrigger Binding="{Binding Path = 'ConfigurationProperty.bitCPIsNumeric'}" Value="False">
<Setter Property="Content" Value="String" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Resources>
</Label>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I am trying to bind a button withing datagrid to a custom command.
But for some reason, the command dont get executed.
This is the XAML:
<Grid Name="LayoutRoot">
<ListView Name="ListView1"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=id,Mode=OneWay}"
Margin="-6,0,-6,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=name,Mode=OneWay}"
Margin="-6,0,-6,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Path" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=path,Mode=OneWay}"
Margin="-6,0,-6,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Update" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding id}" Command="{Binding ???????????}" Content="Edit"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
What should I write instead of the ?????????? in order to show a message with the ID.
Found a solution here:
WPF DataGrid - Button in a column, getting the row from which it came on the Click event handler
I think problem is in Cells DataContext. Within DataTemplate it does not see parent context. Try to add your command to Resources and do something like:
<DataTemplate>
<Button CommandParameter="{Binding id}" Command="{StaticResource yourCommand}" Content="Edit"/>
</DataTemplate>
Create a command Inherit from ICommand interface.
public class ShowMessageWithIdCommand : ICommand
{
// Implement it members
}
in your viewmodel:
public ICommand ShowMessageWithIdCommand
{
get{return new ShowMessageWithIdCommand(...);}
}
Then your ????? will be: ShowMessageWithIdCommand
More detail you can find here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx