• Labeled ContentControl & LabeledControl【项目】


    LabeledTextBoxControl:

    C#定义
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace Halliburton.Castor.Controls
    {
         class LabeledTextBoxControl : Control
        {
            public static readonly DependencyProperty LabelProperty;
            public static readonly DependencyProperty TextProperty;
            public static readonly DependencyProperty MaxLengthProperty;
    
            static LabeledTextBoxControl()
            {
                
                Type ownerType = typeof(LabeledTextBoxControl);
    
                //FrameworkPropertyMetadata defaultStyleKeyMetadata = new FrameworkPropertyMetadata();
                //defaultStyleKeyMetadata.DefaultValue = ownerType;
                //DefaultStyleKeyProperty.OverrideMetadata(ownerType, defaultStyleKeyMetadata);
    
                FrameworkPropertyMetadata labelMetadata = new FrameworkPropertyMetadata(default(String), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
                LabelProperty = DependencyProperty.Register("Label", typeof(string), ownerType, labelMetadata);
    
                FrameworkPropertyMetadata textMetadata = new FrameworkPropertyMetadata(default(String), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
                
                TextProperty = DependencyProperty.Register("Text", typeof(string), ownerType, textMetadata);
    
                FrameworkPropertyMetadata maxLengthMetadata = new FrameworkPropertyMetadata();
                MaxLengthProperty = DependencyProperty.Register("MaxLength", typeof(int), ownerType, maxLengthMetadata);
            }
    
            public string Label
            {
                get
                {
                    return (string)GetValue(LabelProperty);
                }
    
                set
                {
                    SetValue(LabelProperty, value);
                }
            }
    
            public string Text
            {
                get
                {
                    return (string)GetValue(TextProperty);
                }
    
                set
                {
                    SetValue(TextProperty, value);
                }
            }
    
            public int MaxLength
            {
                get
                {
                    return (int)GetValue(MaxLengthProperty);
                }
    
                set
                {
                    SetValue(MaxLengthProperty, value);
                }
            }
        }
    }
    Template定义:

    <
    Style x:Key="LabeledTextBoxControl_Style" TargetType="{x:Type controls:LabeledTextBoxControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type controls:LabeledTextBoxControl}"> <Grid MaxHeight="30" VerticalAlignment="Center"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock x:Name="Label" Grid.Column="0" VerticalAlignment="Center" FontSize="9pt" FontWeight="Normal" Foreground="#FF221F1F" Style="{StaticResource Univers57_Condensed}" Text="{TemplateBinding Label}" TextAlignment="Right" /> <TextBox x:Name="Content" Grid.Column="1" Width="Auto" Height="22" Margin="4,0,30,0" VerticalAlignment="Center" Style="{StaticResource ExpandableTextBox_Style}" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
    使用过程
    
    <controls:LabeledTextBoxControl Grid.Row="0"
                                                        Grid.Column="0"
                                                        KeyboardNavigation.TabIndex="0"
                                                        Label="Project :"
                                                        MaxLength="30"
                                                        Style="{StaticResource LabeledTextBoxControl_Style}"
                                                        Text="{Binding Path=ProjectName,
                                                                       UpdateSourceTrigger=PropertyChanged}" />

    下面是一个用作LabeledCommentControl的例子,其实还是使用的LabeledTextBoxControl,区别是个头大点,可以wrap里面的text

    <Style x:Key="LabeledCommentControl_Style" TargetType="{x:Type controls:LabeledTextBoxControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type controls:LabeledTextBoxControl}">
                        <Grid VerticalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock x:Name="Label"
                                       Grid.Column="0"
                                       Margin="0"
                                       VerticalAlignment="Top"
                                       FontSize="9pt"
                                       FontWeight="Normal"
                                       Foreground="#FF221F1F"
                                       Style="{StaticResource Univers57_Condensed}"
                                       Text="{TemplateBinding Label}"
                                       TextAlignment="Right" />
                            <TextBox x:Name="Content"
                                     Grid.Column="1"
                                     Width="Auto"
                                     Height="Auto"
                                     Margin="4,0,30,0"
                                     AcceptsReturn="True"
                                     Style="{StaticResource ExpandableTextBox_Style}"
                                     Text="{Binding Text,
                                                    UpdateSourceTrigger=PropertyChanged,
                                                    RelativeSource={RelativeSource TemplatedParent}}"
                                     TextWrapping="Wrap" />
                        </Grid>
    
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

     LabeledDatePickerControl:

    <controls:LabeledDatePickerControl Grid.Row="2"
                                                           Grid.Column="1"
                                                           KeyboardNavigation.TabIndex="7"
                                                           Label="Date :"
                                                           Style="{StaticResource LabeledDatePicker_Style}" />
    <Style x:Key="LabeledDatePicker_Style" TargetType="{x:Type controls:LabeledDatePickerControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type controls:LabeledDatePickerControl}">
                        <Grid MaxHeight="30" VerticalAlignment="Center">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock x:Name="Label"
                                       Grid.Column="0"
                                       VerticalAlignment="Center"
                                       FontSize="9pt"
                                       FontWeight="Normal"
                                       Foreground="#FF221F1F"
                                       Style="{StaticResource Univers57_Condensed}"
                                       Text="{TemplateBinding Label}"
                                       TextAlignment="Right" />
                            <DatePicker Grid.Column="1"
                                        Margin="4,0,30,0"
                                        VerticalAlignment="Center"
                                        SelectedDateFormat="Long"
                                        Style="{StaticResource Base_DatePicker_Style}"
                                        Text="{Binding Path=ProjectDate,
                                                       Mode=TwoWay,
                                                       UpdateSourceTrigger=PropertyChanged}" />
                        </Grid>
    
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace Halliburton.Castor.Controls
    {
        class LabeledDatePickerControl : ContentControl
        {
            public static readonly DependencyProperty LabelProperty;
    
            static LabeledDatePickerControl()
            {
                LabelProperty = DependencyProperty.Register("Label", typeof(string),
                                typeof(LabeledDatePickerControl),
                                new FrameworkPropertyMetadata(string.Empty));
            }
            public string Label
            {
                get { return (string)GetValue(LabelProperty); }
                set { SetValue(LabelProperty, value); }
            }
        }
    }

    LabeldControl:

    可以装任何controls在label的后面,包括joey的valueEditor,valuedComboBox等

    下面是FluidViscosity的例子:

    这里例子里的FluidViscosity是用我自己写的LabeledControl 做的,而CasedHoleID是直接用Joey的LabeledMeasurementEditor做的,他里面也有Field的属性和我的label的意思一样。注意可以把任何一个ControlTemplate类型放在一个ContentControl的Template属性下。

    这个是CasedHoleID的例子

        <ControlTemplate x:Key="HoleID_Template">
            <controls:LabeledMeasurementEditor x:Name="HoleID"
                                               MinWidth="120"
                                               DisplayPrecision="3"
                                               Field="HoleID"
                                               StorageMaximumValue="50"
                                               StorageMinimumValue="0.001"
                                               StorageValue="{Binding HoleID,
                                                                      UpdateSourceTrigger=PropertyChanged}" />
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding IsOpenHole}" Value="True">
                    <Setter TargetName="HoleID" Property="Label" Value="Open Hole ID : " />
                </DataTrigger>
    
                <DataTrigger Binding="{Binding IsOpenHole}" Value="False">
                    <Setter TargetName="HoleID" Property="Label" Value="Cased Hole ID : " />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    View Code
    <ContentControl Grid.Row="2"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                MinWidth="120"
                                Focusable="False"
                                Template="{StaticResource HoleID_Template}" />
    View Code

    下面的是FluidViscosity的部分

    <controls:LabeledControl Grid.Row="1"
                                         Grid.Column="0"
                                         Grid.ColumnSpan="2"
                                         MinWidth="120"
                                         Margin="20,0,-30,0"
                                         VerticalAlignment="Bottom"
                                         Label="Fluid Viscosity "
                                         Style="{StaticResource HorizontalLabeledControl_Style}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="25" />
                        </Grid.ColumnDefinitions>
                        <UnitLibrary_Controls:ValueEditor VerticalAlignment="Bottom"
                                                          DisplayPrecision="3"
                                                          StorageMaximumValue="10000"
                                                          StorageMinimumValue="0.001"
                                                          StorageValue="{Binding FluidViscosity,
                                                                                 TargetNullValue=0,
                                                                                 UpdateSourceTrigger=PropertyChanged}"
                                                          Style="{StaticResource ValueEditor_Style}" />
                        <TextBlock Grid.Column="1"
                                   Margin="2,0,0,0"
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Bottom"
                                   FontSize="9pt"
                                   Foreground="#FF221F1F"
                                   Style="{StaticResource Univers57_Condensed}"
                                   Text="cp" />
    
                    </Grid>
                </controls:LabeledControl>
    <Style x:Key="HorizontalLabeledControl_Style" TargetType="{x:Type controls:LabeledControl}">
            <Setter Property="Template" Value="{StaticResource HorizontalLabeledControl_Template}" />
            <Setter Property="LabelStyle" Value="{StaticResource Univers57_Condensed}" />
        </Style>
    <ControlTemplate x:Key="HorizontalLabeledControl_Template" TargetType="{x:Type controls:LabeledControl}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0"
                           VerticalAlignment="Center"
                           FontSize="9pt"
                           FontWeight="Normal"
                           Foreground="#FF221F1F"
                           Style="{TemplateBinding LabelStyle}"
                           Text="{Binding Label,
                                          RelativeSource={RelativeSource TemplatedParent},
                                          StringFormat={0}:}"
                           TextAlignment="Right" />
                <ContentPresenter Grid.Column="1"
                                  Margin="4,0,30,0"
                                  VerticalAlignment="Center" />
            </Grid>
        </ControlTemplate>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace Halliburton.Castor.Controls
    {
    
        class LabeledControl:ContentControl
        {
            public static readonly DependencyProperty LabelProperty;
            public static readonly DependencyProperty LabelStyleProperty;
            static LabeledControl()
            {
                Type ownerType = typeof(LabeledControl);
    
                LabelProperty = DependencyProperty.Register("Label", typeof(string), ownerType);
                LabelStyleProperty = DependencyProperty.Register("LabelStyle", typeof(Style), ownerType);
    
                FocusableProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(false));
            }
            public string Label
            {
                get { return (string)GetValue(LabelProperty); }
                set { SetValue(LabelProperty, value); }
            }
            public Style LabelStyle
            {
                get { return (Style)GetValue(LabelStyleProperty); }
                set { SetValue(LabelStyleProperty, value); }
            }
        }
    }

     另外一个带两个RaidoButton的例子:

    <controls:LabeledControl Grid.Row="3"
                                         Grid.Column="6"
                                         Grid.ColumnSpan="3"
                                         Margin="40,0,-30,0"
                                         Label="End Rings "
                                         Style="{StaticResource HorizontalLabeledControl_Style}"
                                         Visibility="{Binding ElementName=BondedToPipe,
                                                              Path=IsChecked,
                                                              Converter={StaticResource boolToVisibilityConverter}}">
                    <StackPanel HorizontalAlignment="Left"
                                VerticalAlignment="Center"
                                Orientation="Horizontal"
                                Visibility="{Binding ElementName=BondedToPipe,
                                                     Path=IsChecked,
                                                     Converter={StaticResource boolToVisibilityConverter}}">
                        <RadioButton Margin="3,0,0,0"
                                     Content="Standard"
                                     IsChecked="{Binding ToolInfo.IsStandard}"
                                     Style="{StaticResource RadioButton_Style}" />
                        <RadioButton Margin="10,0,0,0"
                                     Content="K2 End-Ring"
                                     IsChecked="{Binding ToolInfo.IsK2}"
                                     Style="{StaticResource RadioButton_Style}" />
                    </StackPanel>
                </controls:LabeledControl>
  • 相关阅读:
    PHP导出数据到淘宝助手CSV的方法分享
    创业日志:壹百款购物客户中心正式上线啦!
    THINKPHP+JS缩放图片式截图的实现
    入园3年来的感慨
    CentOS 5 全攻略 一步一步配置详解
    创业日记:进入电子商务领域,需未雨绸缪,更要步步谨慎
    IT商悟读书笔记
    震惊的事情一波接一波的,找自己的FREE
    创业日记:微团队,技术应用思考
    博客园我回来了!
  • 原文地址:https://www.cnblogs.com/shawnzxx/p/3204088.html
Copyright © 2020-2023  润新知