获取WPF中的“设置用户控件属性”

时间:2014-10-17 05:29:31

标签: c# wpf button properties user-controls

您好我访问用户控件属性时遇到问题。我的用户控件如下所示:

 <UserControl.Resources>
        <Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <ControlTemplate.Resources>
                            <Storyboard x:Key="OnChecking">
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="25"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                            <Storyboard x:Key="OnUnchecking">
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                                    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
                                </DoubleAnimationUsingKeyFrames>

                                <ThicknessAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="slider" Storyboard.TargetProperty="(FrameworkElement.Margin)">
                                    <SplineThicknessKeyFrame KeyTime="00:00:00.3000000" Value="1,1,1,1"/>
                                </ThicknessAnimationUsingKeyFrames>
                            </Storyboard>
                        </ControlTemplate.Resources>

                        <Border Name="Border">
                            <DockPanel x:Name="dockPanel">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}" RecognizesAccessKey="True" VerticalAlignment="Center"/>
                                <Grid Width="50" x:Name="grid">
                                    <TextBlock Text="ON" TextWrapping="Wrap" FontWeight="Bold" FontSize="12" HorizontalAlignment="Right" Margin="0,0,3,0"/>
                                    <TextBlock HorizontalAlignment="Left" Margin="2,0,0,0" FontSize="12" FontWeight="Bold" Text="OFF" TextWrapping="Wrap"/>
                                    <Border HorizontalAlignment="Left" x:Name="slider" Width="23" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3" RenderTransformOrigin="0.5,0.5" Margin="1,1,1,1">
                                        <Border.RenderTransform>
                                            <TransformGroup>
                                                <ScaleTransform ScaleX="1" ScaleY="1"/>
                                                <SkewTransform AngleX="0" AngleY="0"/>
                                                <RotateTransform Angle="0"/>
                                                <TranslateTransform X="0" Y="0"/>
                                            </TransformGroup>
                                        </Border.RenderTransform>
                                        <Border.BorderBrush>
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                <GradientStop Color="#FFFFFFFF" Offset="0"/>
                                                <GradientStop Color="#FF4490FF" Offset="1"/>
                                            </LinearGradientBrush>
                                        </Border.BorderBrush>
                                        <Border.Background>
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                <GradientStop Color="#FF8AB4FF" Offset="1"/>
                                                <GradientStop Color="#FFD1E2FF" Offset="0"/>
                                            </LinearGradientBrush>
                                        </Border.Background>
                                    </Border>
                                </Grid>
                            </DockPanel>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="False">
                                <Trigger.ExitActions>
                                    <BeginStoryboard Storyboard="{StaticResource OnUnchecking}" x:Name="OnUnchecking_BeginStoryboard"/>
                                </Trigger.ExitActions>
                                <Trigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource OnChecking}" x:Name="OnChecking_BeginStoryboard"/>
                                </Trigger.EnterActions>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <CheckBox x:Name="CheckBox1" HorizontalAlignment="Center" 
                  Style="{DynamicResource CheckBoxStyle1}" 
                  VerticalAlignment="Center"
                  Checked="CheckBox1_OnChecked"
                  Unchecked="CheckBox1_OnUnchecked"/>
    </Grid>

在后面的代码中(一些大量的getter / setter):

public new Brush Background
        {
            get
            {
                var border = CheckBox1.Template.LoadContent() as Border;
                return border == null ? Brushes.White : border.Background;
            }
            set
            {
                var border = CheckBox1.Template.LoadContent() as Border;
                if (border == null) return;
                border.Background = value;
            }
        }

        public new Thickness BorderThickness 
        {
            get
            {
                var border = CheckBox1.Template.LoadContent() as Border;
                return border == null ? new Thickness(0) : border.BorderThickness;
            }
            set
            {
                var border = CheckBox1.Template.LoadContent() as Border;
                if (border == null) return;
                border.BorderThickness = value;
            }

        }

        public new Brush BorderBrush
        {
            get
            {
                var border = CheckBox1.Template.LoadContent() as Border;
                return border == null ? Brushes.Transparent : border.BorderBrush;
            }
            set
            {
                var border = CheckBox1.Template.LoadContent() as Border;
                if (border == null) return;
               border.BorderBrush = value;
            }
        }

我也在这样的另一个窗口中使用它(SliderOnOff是我的控件的名字):

 <switch:SliderOnOff x:Name="SliderOnOff" 
                                HorizontalAlignment="Center" 
                                VerticalAlignment="Center" 
                                IsEnabled="True" 
                                Background="Green"
                                BorderThickness="10"
                                BorderBrush="HotPink"
                                IsChecked="True"
                                BorderRadius="5"
                                Checked="SliderOnOff_OnChecked"
                                UnChecked="SliderOnOff_OnUnChecked">

            </switch:SliderOnOff>

我认为它工作正常,因为在xaml中设置属性导致改变控件属性。

BUT !!!!!!

当我尝试以编程方式更改它时(Slider.Background = Brushes.HotPink;),没有任何反应,它不会改变。此部分属性也根本不起作用(xaml中没有,代码中没有):

 public CornerRadius BorderRadius
        {
            get
            {
                var border = CheckBox1.Template.LoadContent() as Border;
                return border == null ? new CornerRadius(0) : border.CornerRadius;
            }
            set
            {
                var border = CheckBox1.Template.LoadContent() as Border;
                if (border == null) return;
                border.CornerRadius = new CornerRadius(value.TopLeft,value.TopRight,value.BottomRight,value.BottomLeft);
            }
        }

你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我认为你应该了解这些变化 看看:http://www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha
您的班级必须实施INotifyPropertyChanged,并在您的&#34; set&#34;部分你必须提出这个事件。 像:

public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
    public KeyInfo SelectedKeyInfo
    {
        get
        { 
            return _SelectedKeyInfo; 
        }
        set
        {
            _SelectedKeyInfo = value;
            OnPropertyChanged("SelectedKeyProducts");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}