我正在尝试创建一个简单的下载管理器,我在使用ProgressBars添加列时遇到问题,它创建了两个具有相同值的列但是有一个列 数字和一个具有进度条值。如何隐藏或删除“Progress1”,但仍保留“Progress2”中的值
这是主要的xaml
<DataGrid.Columns>
<!--#region Program Number -->
<DataGridTemplateColumn SortMemberPath="#" Header="#">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProgramID}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<!--#endregion-->
<!--#region Program Number -->
<DataGridTemplateColumn SortMemberPath="Name" Header="Name">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<!--#endregion-->
<!--#region Program Number -->
<DataGridTemplateColumn SortMemberPath="Size" Header="Size">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBlock Text="{Binding Size}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<!--#endregion-->
<!--#region Progress bar columns -->
<DataGridTemplateColumn SortMemberPath="Progress"
Header="Progress" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ProgressBar Value="{Binding Progress}"
Height="15"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--#endregion-->
</DataGrid.Columns>
</DataGrid>
以下是程序的代码隐藏
public class Programs : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnProperyChanged(string PropertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
private int _programid;
public int ProgramID
{
get { return _programid; }
set
{
_programid = value;
OnProperyChanged($"{nameof(ProgramID)}");
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnProperyChanged($"{nameof(Name)}");
}
}
private double _size;
public double Size
{
get { return _size; }
set
{
_size = value;
OnProperyChanged($"{nameof(Size)}");
}
}
private double _progress;
public double Progress
{
get { return _progress; }
set
{
_progress = value;
OnProperyChanged($"{nameof(Progress)}");
}
}
public ObservableCollection<Programs> ProgramList()
{
ObservableCollection<Programs> programs = new ObservableCollection<Programs>
{
new Programs { _programid = 1, _name = "Program0", _size = 5.1, _progress = 100 },
new Programs { _programid = 2, _name = "Program1", _size = 7.1, _progress = 36.7 },
new Programs { _programid = 3, _name = "Program2", _size = 1.1, _progress = 44.7 },
new Programs { _programid = 4, _name = "Program3", _size = 6.1, _progress = 88.7 },
new Programs { _programid = 5, _name = "Program4", _size = 2.1, _progress = 89.7 },
new Programs { _programid = 6, _name = "Program5", _size = 9.1, _progress = 68.7 },
new Programs { _programid = 7, _name = "Program6", _size = 5.1, _progress = 98.7 },
new Programs { _programid = 8, _name = "Program7", _size = 2.1, _progress = 59.7 },
new Programs { _programid = 9, _name = "Program8", _size = 83.1, _progress = 18.7 }
};
return programs;
}
}
Here is what it look like (第一次发布一个问题,如果有任何遗漏,我很乐意添加)
答案 0 :(得分:0)
问题是列是自动生成的,因为您尚未设置AutoGeneratedColumns="false"
,而true
是默认值。
如果您未将其设置为false
,DataGrid
将为指定的视图模型中的每个媒体资源生成一列。
有关详细信息,请查看DataGrid.AutoGenerateColumns Property
当您将其设置为false时,您只会看到进度条。因此,您必须使用ProgressBar
来指定其他列。
你可以这样吗
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ProgramID}"/>
<DataGridTextColumn Binding="{Binding Name}"/>
<DataGridTextColumn Binding="{Binding Size}"/>
<DataGridTemplateColumn SortMemberPath="Progress" Header="Progress2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ProgressBar Value="{Binding Progress1}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
修改强>
像这样使用DataGridTextColumn
<DataGridTextColumn SortMemberPath="ProgramID"
Header="#"
Binding="{Binding ProgramID}"/>
当您想要显示文字时,而不是TemplateColumn
。更容易;)
你为什么没有看到任何代码的问题是你只设置了
CellEditingTemplate
而不是CellTemplate
。
CellEditingTemplate
是单元格处于编辑模式时显示的模板。
CellTemplate
是当单元格未处于编辑模式时显示的那个。
当你不按自己的方式去做时,你必须改变这个
<DataGridTemplateColumn SortMemberPath="ProgramID" Header="#">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProgramID}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>