Вот один из способов сделать это, в основном то, что я сделал:
- Подкласс ComboBox и добавление свойств зависимостей IsProgressVisible и ProgressValue
- Добавление зеленого прямоугольника в элемент управления ComboBoxшаблон точно за редактируемой областью
- Привязать видимость прямоугольника к IsProgressVisible и ширину прямоугольника (используя ScaleTransform) к ProgressValue
Сначала новый код управления:
public class ProgressCombo : ComboBox
{
public static readonly DependencyProperty IsProgressVisibleProperty =
DependencyProperty.Register("IsProgressVisible", typeof(bool), typeof(ProgressCombo));
public bool IsProgressVisible
{
get { return (bool)GetValue(IsProgressVisibleProperty); }
set { SetValue(IsProgressVisibleProperty, value); }
}
public static readonly DependencyProperty ProgressValueProperty =
DependencyProperty.Register("ProgressValue", typeof(double), typeof(ProgressCombo));
public double ProgressValue
{
get { return (double)GetValue(ProgressValueProperty); }
set { SetValue(ProgressValueProperty, value); }
}
}
Также есть конвертер значений, который мы будем использовать:
public class FromPercentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((double)value) / 100;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Теперь возьмем образец стиля комбинированного блока из MSDN (версия .net 3.5, а не 4) из http://msdn.microsoft.com/en-us/library/ms750638%28VS.90%29.aspx
Добавьте определение xmlns:l
к вашей собственной сборке
Теперь измените <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
на <Style x:Key="{x:Type l:ProgressCombo}" TargetType="l:ProgressCombo">
Измените <ControlTemplate TargetType="l:ComboBox">
На:
<ControlTemplate TargetType="l:ProgressCombo">
<ControlTemplate.Resources>
<BooleanToVisibilityConverter x:Key="Bool2Vis"/>
<l:FromPercentConverter x:Key="FromPercent"/>
</ControlTemplate.Resources>
Найдите строку <ContentPresenter
и добавьте перед ним:
<Rectangle
Fill="LightGreen"
Margin="3,3,23,3"
Visibility="{TemplateBinding IsProgressVisible, Converter={StaticResource Bool2Vis}}">
<Rectangle.RenderTransform>
<ScaleTransform ScaleX="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ProgressValue, Converter={StaticResource FromPercent}}"/>
</Rectangle.RenderTransform>
</Rectangle>
И это все