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>
<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【项目】的更多相关文章
- SILVERLIGHT 应急卫生模拟演练项目之childwindow
项目中经常要用到childwindow 默认SL提供的界面很不好看 也很难适应系统里的要求 单调的界面 木关系 可以我们可以通过BLEND自定义成我们想要的 首先新建立一个SILVERLIGHT 子窗 ...
- ContentControl 与 ViewModel (一)
前阵子有人问我MVVM模式下,在View中嵌套View,切换View.想一想还是写下来吧. 主要就是用到 ContentControl 和 DataTemplate,这算是一种 ViewModel F ...
- WPF入门教程系列(一) 创建你的第一个WPF项目
WPF入门教程系列(一) 创建你的第一个WPF项目 WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知 ...
- 如何参与一个GitHub开源项目
Github作为开源项目的著名托管地,可谓无人不知,越来越多的个人和公司纷纷加入到Github的大家族里来,为开源尽一份绵薄之力.对于个人来讲,你把自己的项目托管到Github上并不表示你参与了Git ...
- DeepLearning.ai学习笔记(三)结构化机器学习项目--week2机器学习策略(2)
一.进行误差分析 很多时候我们发现训练出来的模型有误差后,就会一股脑的想着法子去减少误差.想法固然好,但是有点headlong~ 这节视频中吴大大介绍了一个比较科学的方法,具体的看下面的例子 还是以猫 ...
- 【分享】2017 开源中国新增开源项目排行榜 TOP 100
2017 年开源中国社区新增开源项目排行榜 TOP 100 新鲜出炉! 这份榜单根据 2017 年开源中国社区新收录的开源项目的关注度和活跃度整理而来,这份最受关注的 100 款开源项目榜单在一定程度 ...
- WPF项目学习.三
工具代码记录 版权声明:本文为博主初学经验,未经博主允许不得转载. 一.前言 记录在学习与制作WPF过程中遇到的解决方案. 分页控件的制作,邮件发送,日志代码,excel导入导出等代码的实现过程: 二 ...
- GitHub上个最有意思的项目合集(技术清单系列)
没有1K以上的星星都不好意思推荐给大家!林子大了,啥项目都有,这里给大家搜罗了10个Github上有趣的项目.如果你就着辣椒食用本文,一定会激动的流下泪来...... 1.一行代码没有 | 18k s ...
- ContentControl as CC和ContentPresenter as CP的使用
1.CC为文本控件的父类,它继承为control,所以他是控件, 2.CP继承FrameworkElement,所以他是容器,相当于占位符 3.想让控件中能包含子控件就需要用CP,反之用CC就行.(不 ...
随机推荐
- UVa572 - Oil Deposits
解题思路:好久没写搜索了,练练手,陶冶情操.不多说,直接贴代码: #include<cstdio> #include<cstring> #include<algorith ...
- PubSub的一种实现
今天在浏览JavaScript事件时,复习了下Dean Edward大神的addEvent.突然觉得可以基于他的思路实现一个结构更好的PubSub. 思路也很简单,就是要维护一个类似如下的一个仓库结构 ...
- XSS跨站及利用
(一)软件测试环境以及搭建 测试环境:本地 XAMPP 1.7.1 测试软件:PHP168整站v5.0 软件下载地址 http://down2.php168.com/v2008.rar PHP.ini ...
- WeifenLuo.WinFormsUI.Docking"的使用 z
在伍华聪的博客中,看到布局控件"WeifenLuo.WinFormsUI.Docking",发现的确是一个非常棒的开源控件,用过的人都深有体会,该控件之强大.美观.不亚于商业控件. ...
- Delphi 关闭MDI子窗口
需要在子窗口的onClose事件中吧Action = caFree; 就可以了. procedure Tfrm_aa.FormClose(Sender: TObject; var Action: TC ...
- Linux上修改weblogic的内存大小
我们经常在使用WebLoigc部署应用程序后,发现程序运行速度并不是很快,遇到这种情况我们可以尝试调整启动时分配的内存,设置方法有两种: 一.在../domain/setDomainEnv.sh文件中 ...
- 用python3破解wingIDE
值得注意的是,python2的整除/在python3中变成了//,sha方法细化成了sha1和sha256,所以破解文件需要更改加密方式和整除部分的编码方式,经过修改后,这个文件可以完美演算出破解码, ...
- Win7桌面快捷方式全变成某个软件的图标,然后所有快捷方式都只打开这个图标的软件
电脑真是用到老学老好,之前没有遇到的情况,今天终于碰上了. 由于电脑桌面搜狗浏览器图标总不显示,于是选择快捷方式的打开方式为搜狗浏览器,结果,尼玛呀,全部快捷图标都变成搜狗的. 上网找了一下,双击就搞 ...
- MySQL_PHP学习笔记_2015_0614_PHP传参总结_URL传参_表单传参
1. PHP 传参总结 1.1 url 传参 解析方法(下面两种解读方式均可以): $firstName1 = $_GET['firstName']; $firstName2 = $_RE ...
- android开发中遇到的bug
这种NullPointerException这么解决啊 Activity.dispatchTouchEvent 里try catch一下 参考:http://www.eoeandroid.com/th ...