微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 将自定义依赖项属性绑定到自定义WPF样式

设计继承的Expander时遇到问题.我的目的是在切换按钮和认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>

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

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>

我不确定如何将此依赖项属性进度绑定到样式中ProgressBar中的进度值.

任何帮助,将不胜感激.

解决方法

在样式中,我们可以使用标准Binding和RelativeSource来设置属性.
<ProgressBar Name="ProgressBar"
             Value="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Progress}"
             Minimum="0"
             Maximum="100" />

然后,在窗口中我们只需添加Progress =“50”或绑定到其他地方.

您还需要将Button的背景设置为透明,或者更改某种布局方式,以便查看它.

原文地址:https://www.jb51.cc/csharp/91605.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐