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. (转)每天一个Linux命令(5): rm

    http://www.cnblogs.com/peida/archive/2012/10/26/2740521.html 昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和 ...

  2. Linux Shell编程(5):整数运算

    http://blog.sina.com.cn/s/blog_6db275da0101asmf.html #!/bin/sh let a=$1+$2 b=$[$1+$2] ((c=$1+$2)) d= ...

  3. 《Unix网络编程》卷2 读书笔记 第2章- Posix IPC

    1. 概述 Posix IPC 包括:Posix消息队列.Posix信号量.Posix共享内存区 Posix IPC在访问它们的函数和描述它们的信息上有一些类似点. 本章讲述所有这些共同属性:用于标识 ...

  4. 【转】使用oschina的git服务器

    原文网址:http://blog.csdn.net/zengraoli/article/details/24975551 1.概要 其实oschina的git服务器与github的差不多,不过既然是中 ...

  5. Android高手进阶教程(五)之----Android 中LayoutInflater的使用!

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://weizhulin.blog.51cto.com/1556324/311450 大 ...

  6. [Papers]NSE, $u$, Lorentz space [Bjorland-Vasseur, JMFM, 2011]

    $$\bex \int_0^T\frac{\sen{\bbu}_{L^{q,\infty}}^p}{\ve+\ln \sex{e+\sen{\bbu}_{L^\infty}}}\rd s<\in ...

  7. Effective c++ 小结

    来源:http://www.cnblogs.com/feisky/archive/2009/11/04/1595990.html 最近又重新看了Effective C+,不过到现在还是有好多地方不懂的 ...

  8. Drupal 7.23:函数drupal_alter()注释

    /** * Passes alterable variables to specific hook_TYPE_alter() implementations. * * This dispatch fu ...

  9. 图的邻接表存储表示(C)

    //---------图的邻接表存储表示------- #include<stdio.h> #include<stdlib.h> #define MAX_VERTEXT_NUM ...

  10. DOM笔记(八):JavaScript执行环境和垃圾收集

    一.执行环境 在有关于JavaScript对象或者this的指向问题时,脱离不了的另外一个概念就是执行环境,即上下文环境.执行环境在JavaScript是一个 很重要的概念,因为它定义了变量或函数有权 ...