WPF C# 进度条绑定到数据网格行

问题描述 投票:0回答:1

在我的数据网格中有一列有进度条,当我引用给定数据网格中的特定行时,每个进度条都必须独立于其他进度条进行更改。

当尝试引用某行中的某个进度条时,没有任何反应,我这样做是希望它们至少立即开始改变。

 private void ColorRow(System.Windows.Controls.DataGrid dg)
        {
            DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(cmdvm.NumberOfFrame);
            
            if (row != null ) 
            { 
                if (cmdvm.NumberOfFrame > 0)
                {
                    row.Background = brush; 
                    cmdvm.PercentageOfFrame = registers[61];

                    ProgressBar progressbar = (ProgressBar)dg.FindName("progressbar");
                    progressbar.Value = registers[61];

                }
            }
        }

XAML:


        <DataGrid x:Name="Datagrid_CmdLines" SelectionUnit="Cell" HeadersVisibility="None" ItemsSource="{Binding CMD_Array}" Margin="52,308,0,0" AutoGenerateColumns="False" Width="635" Height="393" HorizontalAlignment="Left" VerticalAlignment="Top" Background="#FFEEEEEE" Grid.Row="1">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="IsHitTestVisible" Value="False"/>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="150">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <ProgressBar x:Name="progressbar" Value="{Binding PercentageOfFrame, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Minimum="0" Maximum="100" Height="20"/>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

还有我的视图模型:

        public class CMD_VM : INotifyPropertyChanged
        {
            private string[]? _cmd_array;
            private int _numberOfFrame;
            private int _percentage;

            public string[] CMD_Array
            {
                get
                {
                    return _cmd_array!;
                }
                set
                {
                    _cmd_array = value;
                    NotifyPropertyChanged("CMD_Array");
                }
            }
            
            public int NumberOfFrame
            {
                get
                {
                    return _numberOfFrame;
                }
                set
                {
                    _numberOfFrame = value;
                    NotifyPropertyChanged("NumberOfFrame");
                }
            }

            public int PercentageOfFrame
            {
                get
                {
                    return _percentage;
                }
                set
                {
                    _percentage = value;
                    NotifyPropertyChanged("PercentageOfFrame");
                }
            }

            public event PropertyChangedEventHandler? PropertyChanged;
            private void NotifyPropertyChanged(String propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

如何分别处理每个进度条?

c# wpf data-binding binding progress-bar
1个回答
0
投票

您不需要在绑定中指定相对源:

<DataTemplate DataType="{x:Type viewModel:CMD_VM}">
  <Grid>
    <ProgressBar Value="{Binding PercentageOfFrame}" Height="20" />
  </Grid>
</DataTemplate>

应该这样做。

© www.soinside.com 2019 - 2024. All rights reserved.