繁体   English   中英

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

[英]Get Set User Control Properties in WPF

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

 <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中设置属性会导致控件属性的更改。

但!!!!!!

当我尝试以编程方式更改它时(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);
            }
        }

你能帮我吗?

我认为您的ui不必被告知有关更改。
看看: http : //www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha
您的类必须实现INotifyPropertyChanged,并且在“设置”部分中必须引发该事件。 喜欢:

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));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM