完整的wpf自定义步骤条组件代码,样式
时间: 2023-07-09 11:23:34 浏览: 151
wpf完整实例代码
4星 · 用户满意度95%
下面是一个完整的WPF自定义步骤条组件的代码和样式。这个自定义步骤条组件包括了多种状态,包括已完成、进行中和未完成状态。
首先,创建一个名为CustomStepBar的控件,继承自Control。这个控件包含一个ItemsControl,用于显示步骤条的每个步骤。步骤条的状态是通过一个枚举类型StepStatus来表示的。
在控件的模板中,使用ItemsControl来绑定步骤的集合,并为每个步骤使用DataTemplate定义外观。在DataTemplate中,使用Border来显示步骤的背景,并使用TextBlock来显示步骤的文本。使用Trigger或MultiDataTrigger来根据步骤的状态来更改背景颜色或其他外观属性。
最后,添加一些自定义属性和事件,例如CurrentStep属性,用于指定当前步骤,并在控件中使用它来更改步骤的状态。
下面是完整的代码和样式:
CustomStepBar.xaml.cs
```csharp
public class CustomStepBar : Control
{
static CustomStepBar()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomStepBar), new FrameworkPropertyMetadata(typeof(CustomStepBar)));
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable<Step>), typeof(CustomStepBar), new PropertyMetadata(null));
public IEnumerable<Step> ItemsSource
{
get { return (IEnumerable<Step>)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty CurrentStepProperty =
DependencyProperty.Register("CurrentStep", typeof(int), typeof(CustomStepBar), new PropertyMetadata(0, OnCurrentStepChanged));
public int CurrentStep
{
get { return (int)GetValue(CurrentStepProperty); }
set { SetValue(CurrentStepProperty, value); }
}
private static void OnCurrentStepChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CustomStepBar stepBar = d as CustomStepBar;
if (stepBar != null && stepBar.ItemsSource != null)
{
int currentStep = (int)e.NewValue;
foreach (Step step in stepBar.ItemsSource)
{
if (step.StepNumber == currentStep)
{
step.Status = StepStatus.InProgress;
}
else if (step.StepNumber < currentStep)
{
step.Status = StepStatus.Completed;
}
else
{
step.Status = StepStatus.NotStarted;
}
}
}
}
}
public class Step
{
public int StepNumber { get; set; }
public StepStatus Status { get; set; }
}
public enum StepStatus
{
NotStarted,
InProgress,
Completed
}
```
CustomStepBar.xaml
```xaml
<Style TargetType="{x:Type local:CustomStepBar}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomStepBar}">
<ItemsControl ItemsSource="{TemplateBinding ItemsSource}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Width="30" Height="30" Margin="5" CornerRadius="15"
Background="{Binding Path=Status, Converter={StaticResource StepStatusToBackgroundConverter}}">
<TextBlock Text="{Binding Path=StepNumber}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
StepStatusToBackgroundConverter.cs
```csharp
public class StepStatusToBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
StepStatus status = (StepStatus)value;
switch (status)
{
case StepStatus.NotStarted:
return Brushes.LightGray;
case StepStatus.InProgress:
return Brushes.Yellow;
case StepStatus.Completed:
return Brushes.Green;
default:
return Brushes.Transparent;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
```
使用方法:
```xaml
<Window.Resources>
<local:StepStatusToBackgroundConverter x:Key="StepStatusToBackgroundConverter" />
</Window.Resources>
<Grid>
<local:CustomStepBar ItemsSource="{Binding Steps}" CurrentStep="{Binding CurrentStep}" />
</Grid>
```
这里的Steps和CurrentStep都是ViewModel中的属性,用于绑定到CustomStepBar控件上。
阅读全文