突如其来的久违的WPF学习(2)

目录

1.关于第一个问题就是如何定义统一的格式

2.接下来就是怎么引用自定义控件

2.1 首先是定义用户控件

2.2 然后是如何引用这个控件

1.关于第一个问题就是如何定义统一的格式

也就是说<style>的用法,也就是说是自定义控件的基础

注意这个地方定义style的位置在主窗口,也就是说这个不是定义单独的控件!!!!而是应用这个style的情况下可以统一所有的格式!!!

就是说为什么总是找不到源码啊喂!每次都不知道代码片应该放在哪里!求求各位大佬了呜呜呜

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="myBtnStyle" TargetType="{x:Type Button}">
            <Setter Property="Height" Value="72" />
            <Setter Property="Width" Value="150" />
            <Setter Property="Foreground" Value="Red" />
            <Setter Property="Background" Value="Black" />
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Top" />
        </Style>
    </Window.Resources>


    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="369,300,0,0" VerticalAlignment="Top" Width="75" Style="{StaticResource myBtnStyle}"/>
    </Grid>
</Window>

然后需要在button里面引用,也就是上面button引用的那一段!

2.接下来就是怎么引用自定义控件

关于这个部分已经上传了相关代码资源

https://download.csdn.net/download/qq_32623209/86336840

2.1 首先是定义用户控件

自定义用户控件方式是在项目,右键,添加新建项,添加用户控件

<UserControl x:Class="WpfApp2.LimitedInputUserControl"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label Content="{Binding Title}" />
        <Label Grid.Column="1">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding ElementName=txtLimitedInput, Path=Text.Length}" />
                <TextBlock Text="/" />
                <TextBlock Text="{Binding MaxLength}" />
            </StackPanel>
        </Label>
        <TextBox MaxLength="{Binding MaxLength}" Grid.Row="1" Grid.ColumnSpan="2" Name="txtLimitedInput" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" />
    </Grid>
</UserControl>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class LimitedInputUserControl : UserControl
    {
        public LimitedInputUserControl()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public string Title { get; set; }

        public int MaxLength { get; set; }
    }
}

控件的代码是这个样子,主要是设置可访问属性

这中间有个坑,第一次的时候会报错,也就是报InitializeComponent()相关的一个错误,但是这个运行一次即可解决

2.2 然后是如何引用这个控件

引用的时候在主窗体,然后需要声明引用的位置,这个是在本项目底下,不需要特别声明路径

<Window x:Class="WpfApp2.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:WpfApp2"
        xmlns:uc="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <uc:LimitedInputUserControl Title="Enter title:" MaxLength="30" Height="50" />
        <uc:LimitedInputUserControl Title="Enter description:" MaxLength="140" Grid.Row="1" />
    </Grid>
</Window>

 如此如此!这般这般!散会!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值