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>
<ContentControl Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="2"
MinWidth="120"
Focusable="False"
Template="{StaticResource HoleID_Template}" />

下面的是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>

Labeled ContentControl & LabeledControl【项目】的更多相关文章

  1. SILVERLIGHT 应急卫生模拟演练项目之childwindow

    项目中经常要用到childwindow 默认SL提供的界面很不好看 也很难适应系统里的要求 单调的界面 木关系 可以我们可以通过BLEND自定义成我们想要的 首先新建立一个SILVERLIGHT 子窗 ...

  2. ContentControl 与 ViewModel (一)

    前阵子有人问我MVVM模式下,在View中嵌套View,切换View.想一想还是写下来吧. 主要就是用到 ContentControl 和 DataTemplate,这算是一种 ViewModel F ...

  3. WPF入门教程系列(一) 创建你的第一个WPF项目

    WPF入门教程系列(一) 创建你的第一个WPF项目 WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知 ...

  4. 如何参与一个GitHub开源项目

    Github作为开源项目的著名托管地,可谓无人不知,越来越多的个人和公司纷纷加入到Github的大家族里来,为开源尽一份绵薄之力.对于个人来讲,你把自己的项目托管到Github上并不表示你参与了Git ...

  5. DeepLearning.ai学习笔记(三)结构化机器学习项目--week2机器学习策略(2)

    一.进行误差分析 很多时候我们发现训练出来的模型有误差后,就会一股脑的想着法子去减少误差.想法固然好,但是有点headlong~ 这节视频中吴大大介绍了一个比较科学的方法,具体的看下面的例子 还是以猫 ...

  6. 【分享】2017 开源中国新增开源项目排行榜 TOP 100

    2017 年开源中国社区新增开源项目排行榜 TOP 100 新鲜出炉! 这份榜单根据 2017 年开源中国社区新收录的开源项目的关注度和活跃度整理而来,这份最受关注的 100 款开源项目榜单在一定程度 ...

  7. WPF项目学习.三

    工具代码记录 版权声明:本文为博主初学经验,未经博主允许不得转载. 一.前言 记录在学习与制作WPF过程中遇到的解决方案. 分页控件的制作,邮件发送,日志代码,excel导入导出等代码的实现过程: 二 ...

  8. GitHub上个最有意思的项目合集(技术清单系列)

    没有1K以上的星星都不好意思推荐给大家!林子大了,啥项目都有,这里给大家搜罗了10个Github上有趣的项目.如果你就着辣椒食用本文,一定会激动的流下泪来...... 1.一行代码没有 | 18k s ...

  9. ContentControl as CC和ContentPresenter as CP的使用

    1.CC为文本控件的父类,它继承为control,所以他是控件, 2.CP继承FrameworkElement,所以他是容器,相当于占位符 3.想让控件中能包含子控件就需要用CP,反之用CC就行.(不 ...

随机推荐

  1. Android 着色器 Tint 研究

    Tint 这个东西 主要用来减少apk体积的,比如说我现在有一个textview,他的背景图 有两种,一种是当获得焦点时显示的a图,另一种是 失去焦点时显示的b图. 相信大家开发的时候 这种需求做过很 ...

  2. Linux 客户端 下乱码的解决方法

    最近使用xshell登陆英文版redhat,由于某些文件是中文编码,在xshell下显示乱码.折腾了很久终于找到了解决的方法,希望可以对大家有用.其他语言乱码的话,解决方法和此类似! 首先检查系统的l ...

  3. “-bash: svn: command not found”

    今天升级了Xcode5.界面更加的清爽了,但是在命令行里SVN也失去了作用了. 当我要更新的时候提示:“-bash: svn: command not found” 解决办法: 1:打开Prefere ...

  4. 自定义View--一个简单地圆形Progress效果

    先看效果图吧 我们要实现一个自定义的再一个圆形中绘制一个弧形的自定义View,思路是这样的: 先要创建一个类ProgressView,继承自View类,然后重写其中的两个构造方法,一个是一个参数的,一 ...

  5. SoapUI Property

    1. Test Suite(Case) Property 选择Test Suite(Case),switch to Custom properties 在request中的引用方式: ${[scope ...

  6. java webservice AXIS

    1. eclipse axis 插件下载地址   http://archive.apache.org/dist/ws/axis2/tools/1_4_1/ 一个是代码生成插件   axis2-ecli ...

  7. 多态.xml

    pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...

  8. python字典概述

    字典 1.    概述 字典是一个无序的数据集合,序列类型用有序的数字键做索引将数据以数组的形式存储. 在字典中能获得的有序集合只能是键的集合或者是值得集合,方法keys()或者value()返回一个 ...

  9. 闭包在python中的应用,translate和maketrans方法详解

    python对字符串的处理是比较高效的,方法很多.maketrans和translate两个方法被应用的很多,但是具体怎么用常常想不起来. 让我们先回顾下这两个方法吧: 1.s.translate(t ...

  10. ipv4头部分析,读书笔记3

    ip头部最长是60字节,前面的20字节是固定的,选项可加上40字节 4位版本号--- 对于ipv4来说呢,就是4 4位头部长度 ---  表示 有多小个32bit(4字节),4位最大表示数是15啦,也 ...