我是 .Net 的新手,我想知道當我單擊 DataGrid 上特定行中的下載按鈕時如何更新進度條。由于解決這個問題的資源有限,我不明白如何實作這一點。
<DataGrid AutoGenerateColumns="False" x:Name="servers" HorizontalAlignment="Center" Height="148" Margin="0,78,0,0" VerticalAlignment="Top" Width="772" PreviewMouseDoubleClick="clientPreview" >
<DataGrid.Columns>
<DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding Id}" Width="50"></DataGridTextColumn>
<DataGridTemplateColumn Header="Progress"
Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ProgressBar
Value="{Binding Progress}"
Minimum="0" Maximum="100" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Action" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Button Background="#FF00FF35" Click="beginDownload">Download</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
生成的 DataGrid 如下所示:

BeginDownload 方法如下所示:
private void beginDownload(object sender, RoutedEventArgs e)
{
Clients selected = servers.SelectedItem as Clients;
if (selected != null)
{
if (selected.Id == 0)
{
MessageBox.Show("Selected Feild is Empty", Title = "Empty Feild Selected");
}
else
{
//Update Progess Bar and Other Methods
}
}
else
{
MessageBox.Show("Selected Field is Empty", Title = "Empty Field Selected");
}
}
我只想知道如何更新特定行中的進度條。例如,如果我放置一個 for 回圈并將進度從 0 更新到 100。如何將整數值系結到進度條?
發現沒有答案的類似問題 -單擊時同一行中的 Wpf Datagrid 更改元素
uj5u.com熱心網友回復:
在 WPF 中非常流行的帶有命令模式的MVVM方法。據此..您需要擁有View和ViewModel ..然后您將能夠使用網格中的“下載”按鈕的命令并將引數傳遞給它。
public class ClientsViewModel : INotifyPropertyChanged
{
public DelegateCommand DownloadCommand {get; private set;}
// ctor
public ClientsVM()
{
DownloadCommand = new DelegateCommand(DownloadExecute));
}
// async method allow to not block UI thread
public async void DownloadExecute(object param)
{
var client = param as Clients;
if (client==null) return;
// TODO: call real code for download
for (int i=0; i<100; i )
{
await Task.Delay(100); // slow changes of progress
client.Progress = i; // if setter of Progress raised NotifyPropertyChanged.. it will update ProgressBar in your DataGrid
}
}
}
要使用您的 DownloadCommand,只需將其系結到 Button 的屬性
<DataGrid AutoGenerateColumns="False" x:Name="servers" HorizontalAlignment="Center" Height="148" Margin="0,78,0,0" VerticalAlignment="Top" Width="772" PreviewMouseDoubleClick="clientPreview">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding Id}" Width="50"></DataGridTextColumn>
<DataGridTemplateColumn Header="Progress"
Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ProgressBar
Value="{Binding Progress}"
Minimum="0" Maximum="100" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Action" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Button Background="#FF00FF35" Command="{Binding DownloadCommand, Mode=OneWay}" CommandParameter="{Binding}">Download</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
uj5u.com熱心網友回復:
我在 Page.xaml.cs 中的解決方案:
Visual vis;
private void beginDownload(object sender, RoutedEventArgs e)
{
Clients selected = servers.SelectedItem as Clients;
if (selected != null)
{
if (selected.Id == 0)
{
MessageBox.Show("Selected Feild is Empty", Title = "Empty Feild Selected");
}
else
{
for (vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
if (vis is DataGridRow) //Find current DataGridRow
{
ProgressChanged();
//Other methods
break;
}
}
}
else
{
MessageBox.Show("Selected Field is Empty", Title = "Empty Field Selected");
}
}
private void ProgressChanged()
{
// Update the progressbar percentage
for (int i = 0; i <= 100; i )
{
((Clients)(((DataGridRow)vis).Item)).Progress = i;
}
}
我的客戶類:
public class Clients : INotifyPropertyChanged
{
private int mProgress;
public Clients()
{
Id = 0;
}
public int Id { get; set; }
public int Progress
{
get { return mProgress; }
set
{
mProgress = value;
OnPropertyChanged("Progress");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
我還有一個問題我的進度條速度太快了,我怎樣才能減慢進度條的速度?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/521346.html
標籤:C#。网wpf数据网格
上一篇:WPFCombobox事件不會觸發(使用MVVM和Expression.Blend)
下一篇:異步/等待的競爭條件,如何解決
