简体   繁体   中英

Wpf Datagrid change element in the same row on click

I am trying to change a progress bar when user clicked the button which is located at the previous cell. But I can't manage to do it.

在此处输入图片说明

When user clicked the Start button in the same row progress bar value must be changed. But I couldn't manage to access that Element. But what important is Exp To Level is not the property of the class binded. it is a DataGridTemplateColumn .


Current state of my work, I can reach the row element and the sender. But I can't go more than this.

    private void start_Button(object sender, RoutedEventArgs e)
    {
        FrameworkElement ownerGui = ((FrameworkElement)sender);
        MaClass obj = ownerGui.DataContext as MaClass;
    }

I want to change the Value of progress bar.


Here is my XAML part

           <DataGrid.Columns>
            <DataGridTemplateColumn Header="Actions">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button Name="start_stop" Content="Start" Click="start_stop_Button"/>
                            <Button Name="log_button" Content="Logs" Click="log_Button"/>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Exp To Level">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <ProgressBar Value="50" Width="auto"/>
                            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Exp To Level</TextBlock>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

You can define the progress bar value property in viewModel and bind to ProgressBar. And on button click, you can update the value of it. Please see the reference code below:

private void start_Button(object sender, RoutedEventArgs e)
{
    YourViewModel yourViewModel = (sender as Button).DataContext as YourViewModel ;
    if (yourViewModel != null)
    {
        yourViewModel.ProgressBarValue = <<The value you want to set>>;
     }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM