c# - 将自定义依赖属性绑定(bind)到自定义 WPF 样式

标签 c# wpf xaml styles controltemplate

我在设计继承的 Expander 时遇到问题。我的意图是在默认扩展器标题中的切换按钮和文本后面有一个进度条。

我有这个 XAML 代码,它在标题中为我提供了进度条。这是一种自定义样式。

<Style x:Key="CurrentScanExpanderStyle" TargetType="{x:Type local:ProgressExpander}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ProgressExpander}">
                    <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
                        <DockPanel>
                            <Grid DockPanel.Dock="Top">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <ProgressBar Name="ProgressBar"/>
                                <ToggleButton FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual}" Margin="1" MinHeight="0" MinWidth="0" x:Name="HeaderSite" Style="{StaticResource ExpanderDownHeaderStyle}" IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"/>
                            </Grid>
                            <ContentPresenter Focusable="false" Visibility="Collapsed" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" x:Name="ExpandSite" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" DockPanel.Dock="Bottom"/>
                        </DockPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <!-- Triggers haven't changed from the default -->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这工作正常,但我无法绑定(bind)控制进度百分比的自定义依赖属性。

    public class ProgressExpander : Expander
{
    static ProgressExpander()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ProgressExpander), new FrameworkPropertyMetadata(typeof(ProgressExpander)));
    }       



    public int Progress
    {
        get { return (int)GetValue(ProgressProperty); }
        set { SetValue(ProgressProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Progress.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ProgressProperty =
        DependencyProperty.Register("Progress", typeof(int), typeof(ProgressExpander), new UIPropertyMetadata(0));


}

这是窗口内的代码:

            <local:ProgressExpander Grid.Row="1" Header="Current Scan" ExpandDirection="Down" x:Name="currentScanExpander" Style="{DynamicResource CurrentScanExpanderStyle}">
                <Canvas Background="SkyBlue" 
                        Name="currentScanCanvas"
                        Height="{Binding ElementName=currentScanExpander, Path=ActualWidth}"
                        />
            </local:ProgressExpander>

我不确定如何将此依赖属性 progress 绑定(bind)到样式中 ProgressBar 中的进度值。

如有任何帮助,我们将不胜感激。

最佳答案

在样式中,我们可以使用带有 RelativeSource 的标准 Binding 来设置属性。

<ProgressBar Name="ProgressBar"
             Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Progress}"
             Minimum="0"
             Maximum="100" />

然后,在窗口中我们只需添加 Progress="50"或绑定(bind)到其他地方。

您还需要将按钮的背景设置为透明,或更改布局的某种方式,以便看到它。

关于c# - 将自定义依赖属性绑定(bind)到自定义 WPF 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/973372/

相关文章:

WPF 数据网格 : Loading_Completed Event?

c# - Windows Phone 8.1 音乐点击事件

xaml - 具有负边距的元素上的 TapGestureRecognizer 不起作用

wpf - RowVirtualization 导致行的背景颜色不正确

c# - 将 'set' 添加到 C# 中的接口(interface)属性

C# - 异步/等待打印顺序

c# - WPF WrapPanel 中的动态项目填充

silverlight - Windows phone 7 滚动查看器问题

c# - 在 C# winforms 中消除继承 "magic"的最佳方法?

c# - 如何从 .NET Core 中的 appsettings.json 读取节值