精华内容
下载资源
问答
  • VB.NET做的一个WPF的Demo,做了个控件。像是Windows图标一样.图片加文字,移动到图片上,图片会放大。这个demo也说明了如何建立一个自定义属性,并把UserControl中的控件属性绑定到自己定义的属性中。
  • Wpf中 使用 Expander 的样式 ,如果需要绑定自己的属性可以继承Expander 并 添加自己需要的属性 ,如果属性较多可以只绑定一个类,注意增加通知。 最后在样式中使用TempleteBinding 来帮顶自己设置的值,不要忘了 要...

    下载连接 https://download.csdn.net/download/sinat_30224769/12366122

     

    Wpf中 使用 Expander 的样式 ,如果需要绑定自己的属性可以继承Expander 并 添加自己需要的属性 ,如果属性较多可以只绑定一个类,注意增加通知。 最后在样式中使用TempleteBinding 来帮顶自己设置的值,不要忘了 要设置Templete的 X:TargetType 继承的类

    样式 资源DefaultStyleDictionary.xaml  我这里只改了一个Down情况的 因为只用到这一种,如果需要对应其他方向的,自己照葫芦画瓢

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:Expander_Demo">
        <!--xmlns:ex="clr-namespace:iBot.Common.CustomControls.ExtendClass"-->
    
    
        <SolidColorBrush x:Key="Expander.MouseOver.Circle.Stroke" Color="#FF5593FF"/>
        <SolidColorBrush x:Key="Expander.MouseOver.Circle.Fill" Color="#FFF3F9FF"/>
        <SolidColorBrush x:Key="Expander.MouseOver.Arrow.Stroke" Color="#FF000000"/>
        <SolidColorBrush x:Key="Expander.Pressed.Circle.Stroke" Color="#FF3C77DD"/>
        <SolidColorBrush x:Key="Expander.Pressed.Circle.Fill" Color="#FFD9ECFF"/>
        <SolidColorBrush x:Key="Expander.Pressed.Arrow.Stroke" Color="#FF000000"/>
        <SolidColorBrush x:Key="Expander.Disabled.Circle.Stroke" Color="#FFBCBCBC"/>
        <SolidColorBrush x:Key="Expander.Disabled.Circle.Fill" Color="#FFE6E6E6"/>
        <SolidColorBrush x:Key="Expander.Disabled.Arrow.Stroke" Color="#FF707070"/>
        <SolidColorBrush x:Key="Expander.Static.Circle.Fill" Color="#FFFFFFFF"/>
        <SolidColorBrush x:Key="Expander.Static.Circle.Stroke" Color="#FF333333"/>
        <SolidColorBrush x:Key="Expander.Static.Arrow.Stroke" Color="#FF333333"/>
    
        <SolidColorBrush x:Key="Expander.Static.Title.Border" Color="#FFD4D7D5"/>
        <SolidColorBrush x:Key="Expander.Static.Title.Background.Expanded.True" Color="#FF057138"/>
        <SolidColorBrush x:Key="Expander.Static.Title.Background.Expanded.False" Color="Transparent"/>
        <SolidColorBrush x:Key="Expander.Static.Title.Foreground.Expanded.True" Color="#FFF7F7F7"/>
        <SolidColorBrush x:Key="Expander.Static.Title.Foreground.Expanded.False" Color="#FF606266"/>
        <SolidColorBrush x:Key="Expander.Static.Message.Foreground.Status.True" Color="#FFD4D7D5"/>
        <SolidColorBrush x:Key="Expander.Static.Message.Foreground.Status.False" Color="#FF3B3C3E"/>
    
        <Style x:Key="ExpanderRightHeaderStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Padding="{TemplateBinding Padding}">
                            <Grid Background="Transparent" SnapsToDevicePixels="False">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="19"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Grid>
                                    <Grid.LayoutTransform>
                                        <TransformGroup>
                                            <TransformGroup.Children>
                                                <TransformCollection>
                                                    <RotateTransform Angle="-90"/>
                                                </TransformCollection>
                                            </TransformGroup.Children>
                                        </TransformGroup>
                                    </Grid.LayoutTransform>
                                    <Ellipse x:Name="circle" Fill="{StaticResource Expander.Static.Circle.Fill}" HorizontalAlignment="Center" Height="19" Stroke="{StaticResource Expander.Static.Circle.Stroke}" VerticalAlignment="Center" Width="19"/>
                                    <Path x:Name="arrow" Data="M 1,1.5 L 4.5,5 L 8,1.5" HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="{StaticResource Expander.Static.Arrow.Stroke}" StrokeThickness="2" VerticalAlignment="Center"/>
                                </Grid>
                                <ContentPresenter HorizontalAlignment="Center" Margin="0,4,0,0" Grid.Row="1" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Top"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="Data" TargetName="arrow" Value="M 1,4.5  L 4.5,1  L 8,4.5"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.MouseOver.Circle.Stroke}"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.MouseOver.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.MouseOver.Arrow.Stroke}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.Pressed.Circle.Stroke}"/>
                                <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.Pressed.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.Pressed.Arrow.Stroke}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.Disabled.Circle.Stroke}"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.Disabled.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.Disabled.Arrow.Stroke}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ExpanderUpHeaderStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Padding="{TemplateBinding Padding}">
                            <Grid Background="Transparent" SnapsToDevicePixels="False">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="19"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid>
                                    <Grid.LayoutTransform>
                                        <TransformGroup>
                                            <TransformGroup.Children>
                                                <TransformCollection>
                                                    <RotateTransform Angle="180"/>
                                                </TransformCollection>
                                            </TransformGroup.Children>
                                        </TransformGroup>
                                    </Grid.LayoutTransform>
                                    <Ellipse x:Name="circle" Fill="{StaticResource Expander.Static.Circle.Fill}" HorizontalAlignment="Center" Height="19" Stroke="{StaticResource Expander.Static.Circle.Stroke}" VerticalAlignment="Center" Width="19"/>
                                    <Path x:Name="arrow" Data="M 1,1.5 L 4.5,5 L 8,1.5" HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="{StaticResource Expander.Static.Arrow.Stroke}" StrokeThickness="2" VerticalAlignment="Center"/>
                                </Grid>
                                <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="Data" TargetName="arrow" Value="M 1,4.5  L 4.5,1  L 8,4.5"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.MouseOver.Circle.Stroke}"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.MouseOver.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.MouseOver.Arrow.Stroke}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.Pressed.Circle.Stroke}"/>
                                <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.Pressed.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.Pressed.Arrow.Stroke}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.Disabled.Circle.Stroke}"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.Disabled.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.Disabled.Arrow.Stroke}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ExpanderLeftHeaderStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Padding="{TemplateBinding Padding}">
                            <Grid Background="Transparent" SnapsToDevicePixels="False">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="19"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Grid>
                                    <Grid.LayoutTransform>
                                        <TransformGroup>
                                            <TransformGroup.Children>
                                                <TransformCollection>
                                                    <RotateTransform Angle="90"/>
                                                </TransformCollection>
                                            </TransformGroup.Children>
                                        </TransformGroup>
                                    </Grid.LayoutTransform>
                                    <Ellipse x:Name="circle" Fill="{StaticResource Expander.Static.Circle.Fill}" HorizontalAlignment="Center" Height="19" Stroke="{StaticResource Expander.Static.Circle.Stroke}" VerticalAlignment="Center" Width="19"/>
                                    <Path x:Name="arrow" Data="M 1,1.5 L 4.5,5 L 8,1.5" HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="{StaticResource Expander.Static.Arrow.Stroke}" StrokeThickness="2" VerticalAlignment="Center"/>
                                </Grid>
                                <ContentPresenter HorizontalAlignment="Center" Margin="0,4,0,0" Grid.Row="1" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Top"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="Data" TargetName="arrow" Value="M 1,4.5  L 4.5,1  L 8,4.5"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.MouseOver.Circle.Stroke}"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.MouseOver.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.MouseOver.Arrow.Stroke}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.Pressed.Circle.Stroke}"/>
                                <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.Pressed.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.Pressed.Arrow.Stroke}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Stroke" TargetName="circle" Value="{StaticResource Expander.Disabled.Circle.Stroke}"/>
                                <Setter Property="Fill" TargetName="circle" Value="{StaticResource Expander.Disabled.Circle.Fill}"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="{StaticResource Expander.Disabled.Arrow.Stroke}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ExpanderHeaderFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border>
                            <Rectangle Margin="0" SnapsToDevicePixels="true" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ExpanderDownHeaderStyle" TargetType="{x:Type local:User1ToggleButton}">
            <Setter Property="Height" Value="24"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:User1ToggleButton}">
                        <Border Padding="{TemplateBinding Padding}">
                            <Grid Background="Transparent" SnapsToDevicePixels="False" ClipToBounds="True">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="61"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Rectangle x:Name="background" Fill="{StaticResource Expander.Static.Title.Background.Expanded.False}" Stroke="{StaticResource Expander.Static.Title.Border}" RadiusX="16" RadiusY="16" Margin="-16,0,0,0" />
                                <Border BorderThickness="1,0,0,0" BorderBrush="{StaticResource Expander.Static.Title.Border}" VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
                                <TextBlock x:Name="title" Text="{TemplateBinding Title}" Foreground="{StaticResource Expander.Static.Title.Foreground.Expanded.False}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                <Ellipse Grid.Column="1" Fill="Red" HorizontalAlignment="Left" Height="12"  VerticalAlignment="Center" Width="12" Margin="5,0,0,0"/>
                                <TextBlock x:Name="message" Grid.Column="1"  Text="{TemplateBinding Message}" Foreground="{StaticResource Expander.Static.Message.Foreground.Status.False}"  Margin="22,0,0,0" VerticalAlignment="Center"/>
                                <Path x:Name="arrow"  Visibility="Collapsed"/>
                                <!--<ContentPresenter Grid.Column="1" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>-->
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="Fill" TargetName="background" Value="{StaticResource Expander.Static.Title.Background.Expanded.True}"/>
                                <Setter Property="Foreground" TargetName="title" Value="{StaticResource Expander.Static.Title.Foreground.Expanded.True}"/>
                            </Trigger>
                            <Trigger Property="Status" Value="true">
                                <Setter Property="Foreground" TargetName="message" Value="{StaticResource Expander.Static.Message.Foreground.Status.True}"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Opacity"  Value="0.8"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Opacity"  Value="0.6"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Opacity"  Value="0.6"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ExpanderStyle1" TargetType="{x:Type local:User1Expander}">
            <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="0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:User1Expander}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="3" SnapsToDevicePixels="true">
                            <DockPanel>
                                <local:User1ToggleButton x:Name="HeaderSite" Title="{TemplateBinding Title}" Status="{TemplateBinding Status}" Message="{TemplateBinding Message}" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" Style="{StaticResource ExpanderDownHeaderStyle}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                <ContentPresenter x:Name="ExpandSite" DockPanel.Dock="Bottom" Focusable="false" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </DockPanel>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="true">
                                <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
                            </Trigger>
                            <Trigger Property="ExpandDirection" Value="Right">
                                <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Right"/>
                                <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Left"/>
                                <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderRightHeaderStyle}"/>
                            </Trigger>
                            <Trigger Property="ExpandDirection" Value="Up">
                                <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Top"/>
                                <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Bottom"/>
                                <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderUpHeaderStyle}"/>
                            </Trigger>
                            <Trigger Property="ExpandDirection" Value="Left">
                                <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Left"/>
                                <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Right"/>
                                <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderLeftHeaderStyle}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
    </ResourceDictionary>

    实现自定义属性 自定义控件

     

    
    
        public class User1Expander : Expander
        {
            public User1Expander()
            {
    
            }
    
            public string Title
            {
                get { return (string)GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty TitleProperty =
                DependencyProperty.Register("Title", typeof(string), typeof(User1Expander), new PropertyMetadata("标题"));
    
            public bool Status
            {
                get { return (bool)GetValue(StatusProperty); }
                set { SetValue(StatusProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty StatusProperty =
                DependencyProperty.Register("Status", typeof(bool), typeof(User1Expander), new PropertyMetadata(false));
    
            public string Message
            {
                get { return (string)GetValue(MessageProperty); }
                set { SetValue(MessageProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty MessageProperty =
                DependencyProperty.Register("Message", typeof(string), typeof(User1Expander), new PropertyMetadata("未填写"));
    
    
    
    
      public class User1ToggleButton : ToggleButton
        {
    
            public string Title
            {
                get { return (string)GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty TitleProperty =
                DependencyProperty.Register("Title", typeof(string), typeof(User1ToggleButton), new PropertyMetadata("标题"));
    
            public bool Status
            {
                get { return (bool)GetValue(StatusProperty); }
                set { SetValue(StatusProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty StatusProperty =
                DependencyProperty.Register("Status", typeof(bool), typeof(User1ToggleButton), new PropertyMetadata(false));
    
            public string Message
            {
                get { return (string)GetValue(MessageProperty); }
                set { SetValue(MessageProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty MessageProperty =
                DependencyProperty.Register("Message", typeof(string), typeof(User1ToggleButton), new PropertyMetadata("未填写"));
    
    
    
        }

    最后 Main

    调用

    <Window x:Class="Expander_Demo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Expander_Demo"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
    
        <Window.Resources>
     <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/iBot.Core.UI.Utils;component/Resources/DefaultStyleDictionary.xaml" />
                    <!--<ResourceDictionary Source="/iBot.Core.UI.Utils;component/Resources/ConvertersDictionary.xaml" />-->
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
        <Grid>
    
            <StackPanel HorizontalAlignment="Left" Height="290" Margin="10,10,0,0" VerticalAlignment="Top" Width="335">
                <local:User1Expander x:Name="expander1"  Header="Expander" Title="目录1" Message="AAAAA" Style="{StaticResource ExpanderStyle1}" IsExpanded="{Binding IsChecked,ElementName=R1}" >
                    <Grid Background="#FFE5E5E5" Height="130"/>
                </local:User1Expander>
                <local:User1Expander x:Name="expander2" Header="Expander"  Title="目录2" Message="bbbb" Status="True" Style="{StaticResource ExpanderStyle1}"  IsExpanded="{Binding IsChecked,ElementName=R2}"   >
                    <Grid Background="#FFE5E5E5" Height="100"/>
                </local:User1Expander>
                <local:User1Expander x:Name="expander3" Header="Expander" Title="目录3" Style="{StaticResource ExpanderStyle1}"  IsExpanded="{Binding IsChecked,ElementName=R3}"  >
                    <Grid Background="#FFE5E5E5"  Height="100"/>
                </local:User1Expander>
                <RadioButton x:Name="R1"/>
                <RadioButton x:Name="R2"/>
                <RadioButton x:Name="R3" IsChecked="True"/>
            </StackPanel>
    
        </Grid>
    </Window>
    

     

    展开全文
  • WPF自定义控件自定义属性绑定后不更新问题 原文:WPF自定义控件自定义属性绑定后不更新问题 需要在绑定时设置属性变更触发 UpdateSourceTrigger=...
    原文: WPF自定义控件的自定义属性绑定后不更新问题

    需要在绑定时设置属性变更触发

    UpdateSourceTrigger=PropertyChanged

    例如:

    <Border CornerRadius="20">
                <Border.Background>
                    <ImageBrush 
                        x:Name="objBackImg" 
                        ImageSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:AnimationVideoPlayer},Path=CoverImage,UpdateSourceTrigger=PropertyChanged}"/>
                </Border.Background>
    </Border>

     

    posted on 2019-04-18 10:13 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

    转载于:https://www.cnblogs.com/lonelyxmas/p/10727964.html

    展开全文
  • 项目开发需要,需要做一个圆形的进度条,但是发现 WPF 自带的进度条控件 ProgressBar 无法直接变成圆形,经过不停的Baidu、Google,终于找到了基于 ProgressBar 构建圆形进度条的方法 ▪ 效果图 ▪ 辅助控件引用 在...

    ▪ 前言

    项目开发需要,需要做一个圆形的进度条,但是发现 WPF 自带的进度条控件 ProgressBar 无法直接变成圆形,经过不停的Baidu、Google,终于找到了基于 ProgressBar 构建圆形进度条的方法

    ▪ 效果图

    image

    ▪ 辅助控件引用

    在改造 ProgressBar 控件中,我们将用到 ControlTemplate 模板功能以及 <ed:Arc> 控件。<ed:Arc> 是一个画圆弧的控件(非 WPF 内置的控件),所以我们必须引用进来,否则使用将编译出错。

    1. .Net 框架的版本必须大于等于 4.0
    2. 在项目的 引用 中右键 -> 添加引用,在 程序集 -> 扩展 里面找到 Microsoft.Expression.Drawing 勾选引用
    3. 在窗体的 xaml 文件里的 标签添加如下代码:xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"

    ▪ 改变 ProgressBar 默认模板(基础)

    在窗体 xaml 文件里加入如下代码:
    <!-- 基于样式的 ControlTemplate 模板资源 -->
    <Window.Resources>
        <Style x:Key="styleProgressBar" TargetType="{x:Type ProgressBar}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="100"/>
            <Setter Property="Maximum" Value="360"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ProgressBar}">
                        <Grid Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Top">
                            <ed:Arc ArcThickness="5" StartAngle="0" Fill="{TemplateBinding Background}" Stretch="None" EndAngle="360"/>
                            <ed:Arc ArcThickness="5" StartAngle="0" Fill="{TemplateBinding Foreground}" Stretch="None" EndAngle="{TemplateBinding Value}"></ed:Arc>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    <ProgressBar Style="{StaticResource styleProgressBar}" Value="240" Foreground="#FF000000" Background="#FFFFFFFF"></ProgressBar>
    

    圆形进度条的值应该是 0 ~ 360 度,所以我们在样式里直接限制 ProgressBar 最大值是 360,宽度和高度根据实际情况设置

    以上代码仅仅是构建了圆形进度条,效果图 上标题、剩余时间、备注并没有

    ▪ 改变 ProgressBar 默认模板(高级)

    ProgressBar 控件并没有标题、剩余时间、备注等属性字段,所以在样式的 ControlTemplate 中就不能直接使用 {TemplateBinding ...} 进行属性值的绑定。

    所以此处我们将使用 ProgressBar 控件的 DataContext 属性进行数据绑定,在样式的 ControlTemplate 中我们就可以直接使用 {Binding ...} 调用数据。

    在窗体 xaml 文件里加入如下代码:
    <!-- 基于样式的 ControlTemplate 模板资源 -->
    <Window.Resources>
        <Style x:Key="styleProgressBar" TargetType="{x:Type ProgressBar}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="125"/>
            <Setter Property="Maximum" Value="360"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ProgressBar}">
                        <StackPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Margin="{TemplateBinding Margin}" SnapsToDevicePixels="True">
                            <Label Height="25" Foreground="#FFFFFFFF" Content="{Binding title}" HorizontalAlignment="Center" VerticalAlignment="Top"></Label>
                            <Grid Width="75" Height="75" HorizontalAlignment="Center" VerticalAlignment="Top">
                                <ed:Arc ArcThickness="5" StartAngle="0" Fill="{TemplateBinding Background}" Stretch="None" EndAngle="360"/>
                                <Label Content="{Binding progress}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Center"></Label>
                                <ed:Arc ArcThickness="5" StartAngle="0" Fill="{TemplateBinding Foreground}" Stretch="None" EndAngle="{TemplateBinding Value}"></ed:Arc>
                            </Grid>
                            <Label Height="25" Foreground="#FFFFFFFF" Content="{Binding content}" HorizontalAlignment="Center" VerticalAlignment="Top"></Label>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    <!-- ProgressBar 在窗体的 cs 文件中动态创建并追加到 uiProgressBars 中 -->
    <WrapPanel x:Name="uiProgressBars"></WrapPanel>
    
    在窗体 cs 文件里加入如下代码:
    // 声明圆形进度条的数据对象
    class ProgressData
    {
        public string title { set; get; }
        public string content { set; get; }
        public string progress { set; get; }
    }
    
    // 此段代码防止在窗体 cs 文件的初始化构造函数即可
    // 此处代码仅仅做一个范例,你可以循环执行以下代码创建多个圆形进度条
    // 也可以通过 WEB SERVICE 获取所需的数据,然后循环创建进度条
    ProgressBar pb = new ProgressBar();
    ProgressData pd = new ProgressData();
    
    pb.Value = 240;
    pb.Style = (Style)FindResource("styleProgressBar");
    pb.Foreground = new SolidColorBrush(Color.FromRgb(0,255,0));
    pb.Background = new SolidColorBrush(Color.FromRgb(255,255,255));
    
    pd.title    = "XX001";
    pd.content  = "线路板";
    pd.progress = "剩余 13 天";
    
    pb.DataContext = pd;
    uiProgressBars.Children.Add(pb);
    
    展开全文
  • wpf自定义控件添加属性

    千次阅读 2018-02-24 17:31:14
    wpf自定义控件添加属性 首先,创建UserControl.xaml文件,命名为MyUserButton,之后改成这种样式 &lt;Button x:Class="UserButtonTest.MyUserButton" xmlns="... ...

    wpf为自定义控件添加属性

    首先,创建UserControl.xaml文件,命名为MyUserButton,之后改成这种样式

    <Button x:Class="UserButtonTest.MyUserButton"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:UserButtonTest"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300"
                 Background="Transparent" BorderThickness="0">
    
        <Grid>
            <Border Height="50" BorderThickness="3" CornerRadius ="5" Background="#FFFFCC" BorderBrush="#FF6633">
                <Border BorderThickness="0">
                    <Viewbox VerticalAlignment="Center" HorizontalAlignment="Center">
                        <TextBlock Name="tb" Text="Default Text"></TextBlock>
                    </Viewbox>
                </Border>
            </Border>
        </Grid>
    </Button>

    打开对应的.cs文件,做如下修改

    
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(MyUserButton), new           PropertyMetadata("TextBox", new PropertyChangedCallback(OnTextChanged)));
    
    public string Text
    {
      get { return (string)GetValue(TextProperty); }
      set { SetValue(TextProperty, value); }
    }
    
    static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        ((MyUserButton)sender).OnValueChanged(args);
    }
    protected void OnValueChanged(DependencyPropertyChangedEventArgs e)
    {
        this.tb.Text = e.NewValue.ToString();
    }

    然后在MainWindow.xaml中引用这个控件

    <local:MyUserButton Text="ddd" Width="200" Height="100" ClickF="MyUserButton_ClickF"></local:MyUserButton>

    运行,效果如下
    这里写图片描述

    但是这样做有问题:Button周围有一大片作用区域,是模板定义得有问题,但如果不这样做的话,MyUserButton.cs中无法找到对应的tb控件。试了好久也不得法。如果你把问题解决了,Please @me。
    这里写图片描述

    展开全文
  • 使用MindFusion.Scheduling for WPF自定义实现资源视图的外观以达到更美观的演示。视图的外观自定义是通过设置Calendar.ResourceViewSettings对象的各种属性来达到的。我们将在银色主题上创建我们的演示,通过降低...
  • 由于刚接触WPF不久,经验不足,走了很多弯路,比如本次遇到的设置自定义控件依赖属性无效问题,该依赖属性没有在自定义控件前台直接使用,只是作为一个标识通过后端代码来控制前端部分控件,依赖属性定义看起来没啥问题,...
  • wpf 各种自定义控件

    2016-08-19 09:54:06
    wpf 自定义控件
  • 基于WpfToolKit 自定义属性编辑

    千次阅读 2016-03-10 17:55:41
    Xceed.Wpf.Toolkit 中对 ITypeEditor进行了封装封装后的类为TypeEditor<T> ,我们可以通过这个类来达到自定义的目的。 属性栏中显示成什么样,可以通过自己定义一个相应的控件来实现,或者使用现有的控件。示例如下...
  • wpf自定义控件属性,可以在xaml里设置,那么怎样在c#里修改属性?我自定义了datepicker的一个属性,现在想用c#来后台修改(当触发时间时),求大牛解答
  • WPF分页自定义控件

    2018-12-29 09:43:14
    WPF分页自定义控件,显示当前页数、每页显示项数下拉选择、显示第m至n项,总页数、总条数、首页、上页、可选页数、下页、末页等
  • WPF自定义控件依赖属性怎么实时变化。
  • WPF 自定义属性

    2019-04-15 10:07:00
    原文:WPF 自定义属性 做了一个自定义控件和一个自定义Grid,里面的元素可以随着绑定属性变化: 效果图(一定滑块): 关键代码: 1、自定义属性代码: public class MyGrid : Grid { public static ...
  • WPF自定义控件

    千次阅读 2018-11-07 20:39:41
    WPF自定义控件
  • 自定义一个简单控件,添加依赖属性,想在xmal中绑定设置依赖属性的值,绑定不起作用求问是怎么回事? ``` public partial class LabelEx : UserControl { public LabelEx() { InitializeComponent();...
  • 原先控件的依赖属性写法如下 public class MyControl : Control { public double MyWidth { get => (double)GetValue(MyWidthProperty); set => SetValue(MyWidthProperty, ...
  • WPF控件自定义TextBox控件

    千次阅读 2019-06-25 09:08:10
    1在现有的控件上进行扩展,增加一些新的属性方法等。 2包括一个代码文件和一个默认的主题文件。 3可以使用样式和模板。 4构建控件库的好方法。 说白了一个就是把现有自带的控件进行一定的封装变成一个新的控件另一个...
  • 控件cs文件 using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Markup; using System.Windows.Media; ...
  • WPF自定义控件之依赖属性

    千次阅读 2013-05-08 16:48:07
    public partial class OmenLevel : UserControl { public OmenLevel() { InitializeComponent(); } #region 属性 public static readonly Dependen
  • 1.我们知道原生控件的样式、属性都是可以进行各种样式设置,触发器绑定的,比如TextBox控件的触发器绑定 <DataGridTemplateColumn x:Name="dgFrameContentData" ...
  • dp dp DependencyProperty的代码片段 wp Expansion classname 类名 ClassName() ClassNamePlaceholder type 属性类型 int property 属性名 MyProperty wpdp 依赖属性名 MyDependencyProperty public $type$ $...
  • // 当外部绑定后,数据变化时,属性会变化,但是界面不会更新 //public static readonly DependencyProperty TextTProperty = // DependencyProperty.Register("TextT", typeof(string ), typeof(UserControl1), new...
  • WPF自定义控件及添加依赖属性

    千次阅读 2019-05-09 13:41:16
    一、问题描述 我们要自定义一个Button,不使用任何原生Button的样式效果,所以,直接继承自UserControl而不是Button,但是又要具备...所以,我们添加相应的依赖属性,并将其绑定到内部Button控件的Command和Comma...
  • 这篇文章的意义是,为wpf自定义控件,定义它的专属属性,这个属性不仅仅可以在c#中使用,也可以在xaml中使用,在xaml中赋值或者绑定。 在wpf开发过程中,总会用到格式各样的控件,但是原生控件远远不能满足实际...
  • 我想在WPF中做一个自定义控件,其中有一个自定义属性HelpText,在主界面用此控件时,指定HelpText的值,然后自定义控件绑定此值,显示出来。 自定义控件:public class TextBoxEx : TextBox { static TextBoxEx...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 9,911
精华内容 3,964
关键字:

wpf控件自定义属性