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. T-SQL备忘(4):分页

    set statistics io on set statistics time on --SQL Server 2012分页方式 select * from Production.Product o ...

  2. windows下mysql 数据库的导入导出

    1.以.sql方式方式导入导出 http://www.360doc.com/content/11/0114/11/2905268_86441355.shtml 2.以.txt方式导入导出 http:/ ...

  3. MySQL与Oracle 差异比较之三函数

    函数 编号 类别 ORACLE MYSQL 注释 1 数字函数 round(1.23456,4) round(1.23456,4) 一样:ORACLE:select round(1.23456,4) ...

  4. 使用Firebug和FirePHP调试PHP

    大家都知道Firebug,可能不知大FirePHP,它也是FireFox插件用来调试PHP的,首先确保你安装了Firebug,然后再去安装FirePHP,这是你会看到Firebug多了一只蓝色的虫: ...

  5. 9、NFC技术:NDEF文本格式解析

    NDEF文本格式规范     不管什么格式的数据本质上都是由一些字节组成的.对于NDEF文本格式来说.这些数据的第1个字节描述了数据的状态,然后若干个字节描述文本的语言编码,最后剩余字节表示文本数据. ...

  6. 《DevOps故障排除:Linux服务器运维最佳实践》读书笔记

    首先,这本书是Linux.CN赠送的,多谢啦~ http://linux.cn/thread-12733-1-1.html http://linux.cn/thread-12754-1-1.html ...

  7. 腾讯2015校招一面、二面、HR面

    其实我目前的理想公司就是腾讯. 内推第三面跪了··· 现在校招. 已面完一面.二面.HR面··· 一面主要问我项目和Linux.网络··· 二面主要问我游戏服务器··· 然后是HR面··· 技术面我都 ...

  8. Spring MVC 问题列表:目录

    学习SpringMVC时遇到不少问题,这里将其汇总. 1.怎么搭建SpringMVC 2.SpringMVC和Spring使用是配置到一个文件中还是两个配置文件 3.SpringMVC接受从前台请求 ...

  9. Python【基础第三篇】

    set集合 s1=set() 集合特点: 访问速度快 没有重复项 collections系列(数据类型容器模块) 一.计数器(Counter) Counter是对字典类型的补充,用于追踪值的出现次数. ...

  10. Jersey Rest服务类型

    在Rest服务中,资源类是接收Rest请求并完成响应的核心类,而资源类由Rest服务的“提供者”来调度的,这一定义类似于自定义Servlet类,该类会奖请求分派给指定的Controller/Actio ...