I have a DataGrid
and each row has a Button
which changes a specific column value of row. I am using Caliburn Micro
and every works perfectly. What I would like to do, is take the Button
outside of the DataGrid
. After selecting multiple rows and then pressing just the one `Button, the column value would change just like before, but this time for all the selected rows. Here are a couple of snippets from my current code:
DataView.xaml:
<DataGrid Grid.Row="1" x:Name="Changes" CanUserAddRows="False"
AutoGenerateColumns="False" AlternatingRowBackground="#FFF7F7F7">
<DataGrid.Columns>
<DataGridTextColumn ... />
<DataGridTextColumn ... />
<DataGridTemplateColumn CellStyle="{StaticResource DataGridCellCentered}" Header="Validation">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Button Margin="20 0 20 0" Visibility="{Binding DataContext.Validated,
Converter={StaticResource BoolToVisConverter}, RelativeSource={RelativeSource AncestorType=DataGridCell}}"
cal:Message.Attach="[Event Click] = [Action Validate($this)]" Cursor="Hand"
cal:Action.TargetWithoutContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}">
Validate
</Button>
<StackPanel Orientation="Horizontal" Margin="20 0 20 0"
Visibility="{Binding DataContext.Validiert,
Converter={StaticResource BoolToVis}, RelativeSource={RelativeSource AncestorType=DataGridCell}}">
<TextBlock>Validated</TextBlock>
<Button Margin="20 0 0 0" Padding="10 0 10 0" FontSize="25" Foreground="#FF2F5778"
cal:Message.Attach="[Event Click] = [Action Unvalidate($this)]" Cursor="Hand"
cal:Action.TargetWithoutContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}" >
⮌
</Button>
</StackPanel>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
DataViewModel.cs:
public void Validate(ChangesModel model)
{
model.Validated= true;
model.ValidateDate = DateTime.Now;
model.ValidatedBy = LoggedUser.loggedUser;
db.CheckValidate(model);
}
public void Unvalidate(ChangesModel model)
{
model.Validated= false;
model.ValidateDate = null;
model.ValidatedBy = null;
db.UnCheckValidate(model);
}
ChangesModel.cs:
public class ChangesModel : INotifyPropertyChanged
{
private bool _validated;
public bool Validated
{
get { return _validated; }
set { _validated= value; NotifyPropertyChanged(); }
}
private DateTime? _validatedate;
public DateTime? ValidateDate
{
get { return _validatedate; }
set { _validatedate= value; NotifyPropertyChanged(); }
}
private string _validatedBy;
public string ValidatedBy
{
get { return _validatedBy; }
set { _validatedBy= value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Does anyone have any suggestion how I would create an action for the Button
so that it fires the Validate
method for each selected row of the DataGrid
?
You could use a RowStyle
to bind the IsSelected
property of the row to a property of your ChangesModel
class:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</DataGrid.RowStyle>
Then you bind the button to an action that just iterates through the selected models, eg:
foreach (var model in Changes)
{
if (model.IsSelected)
{
...
{
}
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.