这是自定义进度条解决方案的完整工作建议。
代码在这里:http://1drv.ms/1QmAVuZ
1 .如果所有步骤的宽度不一样,我更喜欢使用具有列和不同宽度的网格
这些列是根据以下类动态构建的:
public class StepItem
{
public int Length { get; set; }
public int Index { get; set; }
public String Label { get; set; }
public Brush Brush { get; set; }
}
2。我选择实现一个CustomControl并继承ItemsControl
CustomControl,因为我不想处理Progressbar 模板部分的实现。
ItemsControl 因为:
-我想向ItemsSource 属性提供StepItems 的集合
-ItemsControl 可以有一些DataTemplate 作为每个项目的模板
-ItemsControl 可以有任何Panel 如Grid 作为模板呈现项目的集合
3.该组件在 Generic.xaml 中有模板
-layoutGrid 会有“连续的彩虹”
-overlayGrid 将根据进度部分显示在步骤上或完全结束(如果没有进度)
-ItemsPresenter会呈现DataTemplates对应每个StepItem的集合
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ProgressItemsControl}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid x:Name="layoutGrid">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<Grid x:Name="overlayGrid" Width="100" HorizontalAlignment="Right" Background="White"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
4.自定义 ItemsPanel 以使用 Grid(而不是垂直布局)
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate >
<Grid x:Name="stepsGrid" IsItemsHost="True" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
5.在组件的代码后面,列宽的设置
int i = 0;
foreach (StepItem stepItem in ItemsSource)
{
total += stepItem.Length;
var columnDefinition = new ColumnDefinition() { Width = new GridLength(stepItem.Length, GridUnitType.Star) };
stepsGrid.ColumnDefinitions.Add(columnDefinition);
Grid.SetColumn(stepsGrid.Children[i], stepItem.Index);
i++;
}
6.用于声明可监控的依赖属性的代码
(摘录)
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(ProgressItemsControl), new PropertyMetadata(0));
7.组件的使用
<local:CustomProgressBar
x:Name="customProgressBar1"
HorizontalAlignment="Left" Height="50" Margin="32,49,0,0"
VerticalAlignment="Top" Width="379"/>
8.为组件提供数据
private List<StepItem> stepItems = new List<StepItem>{
new StepItem{
Index=0,
Label="Step1",
Length=20,
Brush = new SolidColorBrush(Color.FromArgb(255,255,0,0)),
new StepItem{
Index=4,
Label="Step5",
Length=25,
Brush = new SolidColorBrush(Color.FromArgb(255,0,128,0)),
},
};
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
progressItemsControl1.ItemsSource = stepItems;
}
问候