简体   繁体   English

单击同一行中的WPF Datagrid更改元素

[英]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. 但是Exp To Level重要的不是绑定的类的属性。 it is a DataGridTemplateColumn . 它是一个DataGridTemplateColumn


Current state of my work, I can reach the row element and the sender. 我目前的工作状态,可以到达row元素和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. 我想更改进度栏的Value


Here is my XAML part 这是我的XAML部分

           <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. 您可以在viewModel中定义进度条的value属性,并绑定到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>>;
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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