I implemented this already without using MVVM:
private void Del_btn_Click(object sender, RoutedEventArgs e)
{
try
{
List<string> cid = new List<string>();
foreach (DataRowView rw in Xgrid.SelectedItems)
{
cid.Add(rw[1].ToString());
MessageBox.Show("Deleting " + rw[1].ToString());
}
foreach (string cq in cid)
{
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand("delete_prc", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@Cid";
param1.SqlDbType = SqlDbType.NVarChar;
param1.Value = cq;
cmd.Parameters.Add(param1);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Now I want to do this with MVVM.
I have customers.cs model class in which I have all the fields. Now I don't know how to bind the check box so I am able to do this.
Xaml code:
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding Path=ViewModelBase.CheckAll,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGridRow}}}" Name="delete"/>
</Grid>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="rowNo." Width="60" Binding="{Binding RowNumber}" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="CustomerID" Width="80" Binding="{Binding CustomerID}" IsReadOnly="True"></DataGridTextColumn>
<DataGridTextColumn Header="CompanyName" Width="100" Binding="{Binding CompanyName}"></DataGridTextColumn>
<DataGridTextColumn Header="ContactName" Width="100" Binding="{Binding ContactName}"></DataGridTextColumn>
<DataGridTextColumn Header="ContactTitle" Width="100" Binding="{Binding ContactTitle}"></DataGridTextColumn>
<DataGridTextColumn Header="Address" Width="80" Binding="{Binding Address}"></DataGridTextColumn>
<DataGridTextColumn Header="City" Width="100" Binding="{Binding City}"></DataGridTextColumn>
<DataGridTextColumn Header="Region" Width="80" Binding="{Binding Region}"></DataGridTextColumn>
<DataGridTextColumn Header="PostalCode" Width="100" Binding="{Binding PostalCode}"></DataGridTextColumn>
<DataGridTextColumn Header="Country" Width="100" Binding="{Binding Country}"></DataGridTextColumn>
<DataGridTextColumn Header="Phone" Width="100" Binding="{Binding Phone}"></DataGridTextColumn>
<DataGridTextColumn Header="Fax" Width="100" Binding="{Binding Fax}"></DataGridTextColumn>
</DataGrid.Columns>
So how to integrate this with mvvm. please help me doing this.
I want to know what to define in my viewmodel class and what should be the binding path to do this.
In your DataGrid put a command like and you need to add
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<DataGrid HorizontalAlignment="Left" VerticalAlignment="Top" Height="103" Width="517">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding GetItem}" CommandParameter="{Binding Header,ElementName=id}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
and access the Command in your ViewModel like this
RelayCommand getitem;
public RelayCommand GetItem
{
get
{
if(getitem == null)
{
getitem = new RelayCommand(p=>Executeyourcommand(p));
}
return getitem;
}
}
void Executeyourcommand(object parameter)
{
// parameter is your CustomerID
// do your work here
}
your Command helper class
public class RelayCommand : ICommand
{
readonly Action<object> execute;
readonly Predicate<object> canexecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> Execute, Predicate<object> Canexecute)
{
if(Execute == null)
throw new ArgumentNullException("execute");
execute = Execute;
canexecute = Canexecute;
}
public bool CanExecute(object parameter)
{
if(canexecute == null)
return true;
else
return canexecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
public void Execute(object parameter)
{
execute(parameter);
}
}
and ofc i didnt test the code and i hope this is your looking for
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.