WPF 自定义控件的依赖属性的绑定方法

首先一些人觉得WPF中前台的代码应该在前台创建,尽量不要在后台用代码创建。另外如果前台重复代码过多,编写起来非常繁琐而且修改更是头痛。因此使用用户控件的方法把经常使用的前台模块制作成控件,当然用法和普通控件基本相同。

注意的地方已经用红色标记,代码如下:

 

控件后台代码:

public partial class UC_ReagentWellTextBlock : UserControl
    {
        public UC_ReagentWellTextBlock()
        {
            //界面初始化
            InitializeComponent();

            //我这里需要绑定两个TextBlock,因此将这两个TextBlock进行绑定,绑定的是用于中转的变量,后续看的到.
            Binding bindInformation = new Binding("InformationText") { Source = this };
            this.TextBlockInformatioin.SetBinding(TextBlock.TextProperty, bindInformation);

            Binding bindPosition = new Binding("PositionText") { Source = this };
            this.TextBlockPosition.SetBinding(TextBlock.TextProperty, bindPosition);
        }

        //声明依赖属性.
        public static readonly DependencyProperty PositionTextProperty = DependencyProperty.Register("PositionText", typeof(string), typeof(UC_ReagentWellTextBlock));
        public static readonly DependencyProperty InformationTextProperty = DependencyProperty.Register("InformationText", typeof(string), typeof(UC_ReagentWellTextBlock));

        //声明中转的变量
        public string PositionText
        {
            get { return (string)GetValue(PositionTextProperty); }
            set { SetValue(PositionTextProperty, value); }
        }

        public string InformationText
        {
            get { return (string)GetValue(InformationTextProperty); }
            set { SetValue(InformationTextProperty, value); }
        }
    }

控件前台代码:

  <Grid>
        <Border BorderThickness="1" BorderBrush="Black">
            <Grid>
        <Button Style="{DynamicResource s_ShowButton}" />
        <TextBlock x:Name="TextBlockPosition"
                   Style="{DynamicResource s_Position}"/>
        <TextBlock x:Name="TextBlockInformatioin"
                   Style="{DynamicResource s_Info}"/>
            </Grid>
        </Border>
    </Grid>

窗体调用控件后台:

           Binding bind2 = new Binding(bindStr);
           //数据绑定,等号后面的东西可以不用看,替换成其他的对象即可.
           well3_1.DataContext = IC_LABRARY.Instrument.Instrument.InstrumentRackSample.InstrumentWellSampleArray[2].InstrumentContainerTube.InformationManagementPatient;
           //绑定数据源,这里就是一开始想要实现的和基本控件一样的功能.
           well3_1.SetBinding(UC.UC_ReagentWellTextBlock.PositionTextProperty, bind2);
           well3_1.SetBinding(UC.UC_ReagentWellTextBlock.InformationTextProperty, bind2);


 

窗体调用控件前台:

<Window x:Class="IC_DEMO_UI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myReactionWell="clr-namespace:IC_DEMO_UI.UC"
        Title="MainWindow" Height="588.824" Width="300">
    <Window.Resources>
        <Style x:Key="S_TextBlock_No" TargetType="TextBlock">
            <Setter Property="Foreground"  Value="Blue"/>
            <Setter Property="FontFamily" Value="微软雅黑"/>
            <Setter Property="FontSize" Value="13"/>
            <Setter Property="Margin" Value="0,5,0,0"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
        <Style x:Key="S_TextBlock_Content" TargetType="TextBlock">
            <Setter Property="Foreground"  Value="Blue"/>
            <Setter Property="FontFamily" Value="微软雅黑"/>
            <Setter Property="FontSize" Value="13"/>
            <Setter Property="Margin" Value="0,25,0,0"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
        <Style x:Name="S_Boarder" x:Key="S_Border" TargetType="Border">
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
            <Style x:Key="S_ButtonWell" TargetType="{x:Type Button}">
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="2"/>
            <Setter Property="Width" Value="45"/>
            <Setter Property="Height" Value="45"/>

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse Stroke="Black">
                                <Ellipse.Fill>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="White" Offset="0"/>
                                        <GradientStop Color="#FF048A00" Offset="1"/>
                                    </LinearGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                              RecognizesAccessKey="True"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsFocused" Value="True"/>
                            <Trigger Property="IsDefaulted" Value="True"/>
                            <Trigger Property="IsMouseOver" Value="True"/>
                            <Trigger Property="IsPressed" Value="True"/>
                            <Trigger Property="IsEnabled" Value="False"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid x:Name="G_MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
   <myReactionWell:UC_ReagentWellTextBlock x:Name="well3_1" Grid.Row="0" Grid.Column="2" RenderTransformOrigin="0.424,1.627"/>
   </Grid>
</Window>



温馨提示:CSDN的Blog第一天不允许发带链接的文章,否则将封号哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值