ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

WPF 资源字典中自定义控件Command的使用

2022-06-28 11:32:00  阅读:190  来源: 互联网

标签:控件 自定义 DependencyProperty get SelectedItem object Command SideMenu public


问题:

在WPF中 创建了资源字典来写自定义控件 涉及到Button,CheckBox等一系列带Command属性的操作时,需要将命令绑定到我们的代码中,这时你会想到查找器,DataContext等为按钮设置绑定,特别是在ItemsControl等拥有DataTemplate等多重绑定的,最为烦人

解决:

其实很简单,自定义控件可以直接绑定到代码中,只需要在代码中设置为Static即可,代码如下:

资源字典代码:

<Style TargetType="{x:Type local:SideMenu}">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:SideMenu}">
                        <Border Width="{TemplateBinding Width}" Background="#393D49">
                            <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                                <ItemsControl Padding="0"  Background="Transparent" BorderThickness="0" ItemTemplateSelector="{DynamicResource MenuItemSelector}" ItemsSource="{TemplateBinding ItemsSource}">
                                    <ItemsControl.Resources>
                                        <local:MenuItemDataTemplateSelector x:Key="MenuItemSelector" />
                                        <DataTemplate x:Key="MenuItemsDataTemplate">
                                            <local:NavExpander  Padding="10,0" Background="#282B33" Header="{Binding MenuName}" HeaderBackground="#393D49" IsExpanded="{Binding IsSelected}">
                                                <ItemsControl ItemTemplateSelector="{DynamicResource MenuItemSelector}" ItemsSource="{Binding Children}">
                                                    <ItemsControl.ItemsPanel>
                                                        <ItemsPanelTemplate>
                                                            <StackPanel />
                                                        </ItemsPanelTemplate>
                                                    </ItemsControl.ItemsPanel>
                                                </ItemsControl>
                                            </local:NavExpander>
                                        </DataTemplate>
                                        <DataTemplate x:Key="MenuItemDataTemplate">
                                            <RadioButton Height="40" Padding="20,0" Content="{Binding MenuName}" Command="{x:Static local:SideMenu.CheckCommand}"
                                            x:Name="menuContent" FocusVisualStyle="{x:Null}" Foreground="White" GroupName="Item" IsChecked="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" CommandParameter="{Binding}"  >
                                                <RadioButton.Template>
                                                    <ControlTemplate TargetType="RadioButton">
                                                        <Grid>
                                                            <Border x:Name="bg" Background="Transparent" />
                                                            <Grid x:Name="line"  Width="5"  HorizontalAlignment="Left" Background="#50a0ff" Opacity="0" />
                                                            <ContentPresenter x:Name="dataContent"  Margin="{TemplateBinding Padding}" HorizontalAlignment="Left" VerticalAlignment="Center" IsHitTestVisible="True" Opacity="0.7" />
                                                        </Grid>
                                                        <ControlTemplate.Triggers>
                                                            <Trigger Property="IsChecked" Value="true">
                                                                <Setter TargetName="bg" Property="Background" Value="#242424" />
                                                                <Setter TargetName="dataContent" Property="Opacity" Value="1" />
                                                                <Trigger.EnterActions>
                                                                    <BeginStoryboard>
                                                                        <Storyboard>
                                                                            <DoubleAnimation
                                                                Storyboard.TargetName="line"
                                                                Storyboard.TargetProperty="Opacity"
                                                                To="1"
                                                                Duration="0:0:0.5" />
                                                                        </Storyboard>
                                                                    </BeginStoryboard>
                                                                </Trigger.EnterActions>
                                                                <Trigger.ExitActions>
                                                                    <BeginStoryboard>
                                                                        <Storyboard>
                                                                            <DoubleAnimation
                                                                Storyboard.TargetName="line"
                                                                Storyboard.TargetProperty="Opacity"
                                                                To="0"
                                                                Duration="0:0:0.5" />
                                                                        </Storyboard>
                                                                    </BeginStoryboard>
                                                                </Trigger.ExitActions>
                                                            </Trigger>
                                                            <Trigger Property="IsMouseOver" Value="true">
                                                                <Setter TargetName="bg" Property="Background" Value="#4c4c4c" />
                                                                <Setter TargetName="dataContent" Property="Opacity" Value="1" />
                                                            </Trigger>
                                                        </ControlTemplate.Triggers>
                                                    </ControlTemplate>
                                                </RadioButton.Template>
                                            </RadioButton>
                                        </DataTemplate>
                                    </ItemsControl.Resources>
                                </ItemsControl>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>

留意Command="{x:Static local:SideMenu.CheckCommand}"这一句

逻辑代码:

public class SideMenu : Control
    {
        public object ItemsSource
        {
            get { return (object)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(object), typeof(SideMenu), new PropertyMetadata(default(object)));

        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(object), typeof(SideMenu), new PropertyMetadata(default(object)));
        
        public static DelegateCommand<object> CheckCommand { get; set; }

        public event SelectionChangedEventHandler SelectionChanged;
        public SideMenu()
        {
            CheckCommand = new DelegateCommand<object>(CheckCommandExecute);
        }

        private void CheckCommandExecute(object obj)
        {
            SelectedItem = obj;
            SelectionChanged?.Invoke(this, null);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

    }

注意public static DelegateCommand<object> CheckCommand { get; set; },要用Static修饰

 

标签:控件,自定义,DependencyProperty,get,SelectedItem,object,Command,SideMenu,public
来源: https://www.cnblogs.com/xiaoke-/p/16418849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有