WPF框架嵌套用户控件,显示与切换(详细,代码复制可用)

WPF框架手敲实现嵌套页面,点击显示与切换
下面附上成果图吧,看了之后觉得适用就可以参考一下,否则就不要浪费时间了
在这里插入图片描述
操作时间
1.此篇作为学习记录,是一个WPF框架window嵌套用户控件的使用。点击按钮切换显示嵌套的内容
1.1首先创建MainView.xaml
CommandParameter="MovieView"注:MovieView根据自己View的名称

<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>

                    <!--RadioButton样式-->
                    <Style TargetType="RadioButton" x:Key="NavButtonStyle">
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="RadioButton">
                                    <Border Background="Transparent" CornerRadius="2" Name="back">
                                        <ContentControl Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,5" FontSize="12"/>
                                    </Border>
                                    <!--Triggers按钮样式-->
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsChecked" Value="True">
                                            <Setter TargetName="back" Property="Background" Value="#44FFFFFF"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <!--Background-->
    <Window.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0"/>
            <GradientStop Color="#FF164472" Offset="1"/>
        </LinearGradientBrush>
    </Window.Background>

    <Border Margin="5" CornerRadius="5" x:Name="layout">
        <Border.Effect>
            <DropShadowEffect Color="Gray" ShadowDepth="0" BlurRadius="5" Opacity="0.3" Direction="0"/>
        </Border.Effect>
        <Grid>
            <!--Top样式-->
            <Border Background="#FF103C87" Height="50" VerticalAlignment="Top" CornerRadius="5,5,0,0"/>
            <Grid Margin="5,5">
                <!--NavigationHeight-->
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"/>
                    <RowDefinition Height="390"/>
                    <RowDefinition Height="50"/>
                </Grid.RowDefinitions>
                <!--left-->
                <StackPanel Orientation="Horizontal" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center">
                    <RadioButton Style="{StaticResource NavButtonStyle}" IsChecked="True" Command="{Binding NavChangedCommand}" CommandParameter="MovieView">
                        <WrapPanel>
                            <TextBlock Text="&#xe656;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Margin="0,0,1,3"/>
                            <TextBlock Text="医疗服务" Margin="0,0,3,3" FontSize="15"/>
                        </WrapPanel>
                    </RadioButton>
                    <RadioButton Style="{StaticResource NavButtonStyle}" Command="{Binding NavChangedCommand}" CommandParameter="HealthEducationView">
                        <WrapPanel>
                            <TextBlock Text="&#xe627;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Margin="0,0,3,3"/>
                            <TextBlock Text="健康宣教" Margin="0,0,3,3" FontSize="15"/>
                        </WrapPanel>
                    </RadioButton>
                    <RadioButton Style="{StaticResource NavButtonStyle}" Command="{Binding NavChangedCommand}" CommandParameter="MedicalView">
                        <WrapPanel>
                            <TextBlock Text="&#xe617;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Margin="0,0,3,3"/>
                            <TextBlock Text="影音视听" Margin="0,0,3,3" FontSize="15"/>
                        </WrapPanel>
                    </RadioButton>
                    <RadioButton Style="{StaticResource NavButtonStyle}" Command="{Binding NavChangedCommand}" CommandParameter="LifeServiceView">
                        <WrapPanel>
                            <TextBlock Text="&#xe605;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Margin="0,0,3,3"/>
                            <TextBlock Text="生活服务" Margin="0,0,3,3" FontSize="15"/>
                        </WrapPanel>
                    </RadioButton>
                    <RadioButton Style="{StaticResource NavButtonStyle}" Command="{Binding NavChangedCommand}" CommandParameter="HealthView">
                        <WrapPanel>
                            <TextBlock Text="&#xe68d;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Margin="0,0,3,3"/>
                            <TextBlock Text="健康专区" Margin="0,0,3,3" FontSize="15"/>
                        </WrapPanel>
                    </RadioButton>
                </StackPanel>
                <!--Right-->
                <StackPanel Orientation="Horizontal" Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Center">
                    <RadioButton Style="{StaticResource NavButtonStyle}" Command="{Binding NavChangedCommand}" CommandParameter="PersonalView">
                        <WrapPanel>
                            <TextBlock Text="&#xe604;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Margin="0,0,3,3"/>
                            <TextBlock Text="汤姆汤姆" Margin="0,0,3,3" FontSize="15"/>
                        </WrapPanel>
                    </RadioButton>
                    <RadioButton Style="{StaticResource NavButtonStyle}" Command="{Binding NavChangedCommand}" CommandParameter="SetView">
                        <WrapPanel>
                            <TextBlock Text="&#xe603;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Margin="0,0,3,3"/>
                            <TextBlock Text="深圳" Margin="0,0,3,3" FontSize="15"/>
                        </WrapPanel>
                    </RadioButton>
                    <Label Content="今日雷阵雨 23-26°" FontSize="13" Foreground="#FFFF" VerticalAlignment="Center"/>
                    <Label Content="09:30" FontSize="13" Foreground="#FFFF" VerticalAlignment="Center"/>
                </StackPanel>

                <!--中间嵌套层绑定-->
                <Border VerticalAlignment="Bottom" Grid.Row="1">
                    <ContentControl Content="{Binding MainContent}"/>
                </Border>
            </Grid>

            <!--Bottom-->
            <Border Background="Black" VerticalAlignment="Bottom" Grid.Row="2">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom" >
                    <DockPanel Margin="150,12">
                        <Button BorderThickness="0" Background="Black">
                            <TextBlock Text="&#xe64e;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Foreground="White" Background="Black"/>
                        </Button>
                          </DockPanel>
                    <DockPanel Margin="150,12">
                        <Button BorderThickness="0" Background="Black">
                            <TextBlock Text="&#xeb74;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Foreground="White" Background="Black"/>
                        </Button>
                         </DockPanel>
                    <DockPanel Margin="150,12">
                        <Button BorderThickness="0" Background="Black">
                            <TextBlock Text="&#xea70;" FontFamily="/common/Resources/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" Foreground="White" Background="Black"/>
                        </Button >
                         </DockPanel>
       
                </StackPanel>
            </Border>
        </Grid>
    </Border>

1.2在MainView.xaml按F7去到MainView.xaml.cs页面
实例化会报错,因为没有创建,下一步创建

 public MainView()
        {
            InitializeComponent();
            MainViewModel model = new MainViewModel();
            this.DataContext = model;
        }

1.3创建MainViewModel .cs

class MainViewModel : NotifyBase
    {
        private FrameworkElement _mainContent;
        public FrameworkElement MainContent
        {
            get { return _mainContent; }
            set { _mainContent = value; this.DoNotify(); }
        }
        public CommandBase NavChangedCommand { get; set; }

        public MainViewModel()
        {
            this.NavChangedCommand = new CommandBase();
            this.NavChangedCommand.DoExecute = new Action<object>(DoNavChanged);
            this.NavChangedCommand.DoCanExecute = new Func<object, bool>((o) => true);

            DoNavChanged("HealthEducationView");/*健康宣教*/
            DoNavChanged("MedicalView");/*影音视听*/
            DoNavChanged("LifeServiceView");/*生活服务*/
            DoNavChanged("HealthView");/*健康专区*/
            DoNavChanged("PersonalView");/*个人中心*/
            DoNavChanged("SetView");/*设置*/
            DoNavChanged("MovieView");/*医疗服务*/
        }
        private void DoNavChanged(object obj)
        {
            Type type = Type.GetType("UItemplate.View." + obj.ToString());
            ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
            this.MainContent = (FrameworkElement)cti.Invoke(null);
        }
    }

1.4创建NotifyBase.cs

 class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void DoNotify([CallerMemberName] string propName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }

1.5创建CommandBase.cs

public class CommandBase : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return DoCanExecute?.Invoke(parameter) == true;
        }

        public void Execute(object parameter)
        {
            DoExecute?.Invoke(parameter);
        }

        public Action<object> DoExecute { get; set; }

        public Func<object, bool> DoCanExecute { get; set; }

        public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged?.Invoke(this, new EventArgs());
        }
    }
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 WPF 中实现用户控件复制粘贴功能,可以按照以下步骤进行操作: 1. 创建一个自定义控件,并在该控件中添加一个复制方法和一个粘贴方法。例如: ```csharp public class MyUserControl : UserControl { public void Copy() { // 将控件的内容复制到剪贴板中 } public void Paste() { // 从剪贴板中获取内容,并将其粘贴到控件中 } } ``` 2. 在复制方法中,可以使用 WPF 中的 Clipboard 类将控件的内容复制到剪贴板中。例如: ```csharp public void Copy() { // 将控件的内容复制到剪贴板中 Clipboard.SetDataObject(this.Content); } ``` 3. 在粘贴方法中,可以使用 WPF 中的 Clipboard 类从剪贴板中获取内容,并将其粘贴到控件中。例如: ```csharp public void Paste() { // 从剪贴板中获取内容,并将其粘贴到控件中 IDataObject dataObject = Clipboard.GetDataObject(); if (dataObject.GetDataPresent(typeof(string))) { string text = (string)dataObject.GetData(typeof(string)); this.Content = text; } } ``` 4. 在需要使用该控件的页面或窗口中,可以通过调用该控件复制和粘贴方法来实现复制粘贴功能。例如: ```csharp private void CopyButton_Click(object sender, RoutedEventArgs e) { MyUserControl myControl = this.MyControl; myControl.Copy(); } private void PasteButton_Click(object sender, RoutedEventArgs e) { MyUserControl myControl = this.MyControl; myControl.Paste(); } ``` 以上就是 WPF 中实现用户控件复制粘贴功能的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放荡不羁爱java..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值