RIA Services之商业应用----4 Custom DataForm&ChangeTheme

1.        Custom Dataform用来新增一条记录。
前面那篇文章已经提到使用自定义DataForm来新增记录。可能你会觉得自定义DataForm干嘛,直接使用TextBlockTextBox就行。
自定义一个Dataform可以让数据的验证更容易的实现。
A.       先定义一个CustomRestaurant类,它继承了那面这两个接口。
 
下面那么多属性就是我们需要填写的所有字段。
然后我们添加一个验证规则是Name字段必须不为空且大于4个字符串。
    public string this[string columnName]
        {
            get
            {
                string result = null;
 
                if (columnName == "Name")
                {
                    if (String.IsNullOrEmpty(Name))
                        result = "Firstname has to be set!";
                    else if (Name.Length < 3)
                        result = "Firstname's length has to be at least 5 characters!";
                }
              
 
                return result;
          
 
 
B.       创建一个自定义控件,它的结构如下:
 
这里使用了两个DependencyProperty,第一个是是否编辑,第二个是让TheRestaurant作为它的一个属性。
我们这里使用Resource Dictionary来存放模板信息:
 
定义DataForm的模板如下:
< Style  TargetType ="local:CusRestuarantDataForm">
         < Setter  Property ="Template">
             < Setter.Value >
                 < ControlTemplate  TargetType ="local:CusRestuarantDataForm">
                     < Border  Background ="{ TemplateBinding  Background }"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="3" Width="{TemplateBinding Width}">
                         < Grid >
                             < Grid.RowDefinitions >
                                 < RowDefinition  Height ="Auto"/>
                                 < RowDefinition  Height ="Auto"/>
                                 < RowDefinition  Height ="Auto"/>
                                 < RowDefinition  Height ="Auto"/>
                                 < RowDefinition  Height ="Auto"/>
                                 < RowDefinition  Height ="Auto"/>
                                 < RowDefinition  Height ="Auto"/>
                                 < RowDefinition  Height ="Auto"/>
                                 < RowDefinition  Height ="Auto"/>
                               
                             </ Grid.RowDefinitions >
                             < Grid.ColumnDefinitions >
                                 < ColumnDefinition  Width ="150"/>
                                 < ColumnDefinition  Width ="*"/>
                             </ Grid.ColumnDefinitions >
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="0"
                    Text="Address:" Style="{StaticResource tb}" />
                             < TextBox  Grid.Column ="1"  Grid.Row ="0"                            x:Name="tAddress" Style="{StaticResource tx}"
    Text="{Binding Path=TheRestaurant.Address, Mode=TwoWay, ValidatesOnDataErrors=True}" IsReadOnly="{Binding IsLocked}"/>
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="1"
                    Text="Code:" Style="{StaticResource tb}"/>
                             < TextBox  Grid.Column ="1"  Grid.Row ="1"                            x:Name="tCode" Style="{StaticResource tx}"
                                 Text="{Binding Path=TheRestaurant.Code, Mode=TwoWay, ValidatesOnDataErrors=True}" IsReadOnly="{Binding IsLocked}"/>
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="2"                              Text="ContactName:" Style="{StaticResource tb}"/>
                             < TextBox  Grid.Column ="1"  Grid.Row ="2"                            x:Name="tContactName" Style="{StaticResource tx}"
                                 Text="{Binding Path=TheRestaurant.ContactName, Mode=TwoWay}" IsReadOnly="{Binding IsLocked}" />
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="3"  Text ="ContactTitle:"                     Style="{StaticResource tb}"/>
                             < TextBox  Grid.Column ="1"  Grid.Row ="3"                            x:Name="tContactTitle" Style="{StaticResource tx}"
                                Text="{Binding Path=TheRestaurant.ContactTitle, Mode=TwoWay,  TargetNullValue=Doctor}" IsReadOnly="{Binding IsLocked}"/>
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="4"
                    Text="Fax:" Style="{StaticResource tb}"/>
                             < TextBox  Grid.Column ="1"  Grid.Row ="4"                            x:Name="tFax" Style="{StaticResource tx}"
                                 Text="{Binding Path=TheRestaurant.Fax, Mode=TwoWay}" IsReadOnly="{Binding IsLocked}"/>
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="5"                              Text="ID:" Style="{StaticResource tb}"/>
                             < TextBox  Grid.Column ="1"  Grid.Row ="5"                            x:Name="tID" Style="{StaticResource tx}"
                                 Text="{Binding Path=TheRestaurant.ID, Mode=TwoWay}"                                     
                                 AcceptsReturn="True" TextWrapping="Wrap" IsReadOnly="{Binding IsLocked}"/>
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="6"                              Text="Name:" Style="{StaticResource tb}"/>
                             < TextBox  Grid.Column ="1"  Grid.Row ="6"                            x:Name="tName" Style="{StaticResource tx}"
                                 Text="{Binding Path=TheRestaurant.Name, Mode=TwoWay,  ValidatesOnDataErrors=True}" IsReadOnly="{Binding IsLocked}"/>
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="7"  Text ="Phone:"                    Style="{StaticResource tb}"/>
                             < TextBox  Grid.Column ="1"  Grid.Row ="7"                            x:Name="tPhone" Style="{StaticResource tx}"
                                 Text="{Binding Path=TheRestaurant.Phone, Mode=TwoWay,  ValidatesOnDataErrors=True}" IsReadOnly="{Binding IsLocked}"/>
 
                             < TextBlock  Grid.Column ="0"  Grid.Row ="8"                              Text="Region:"
                        Style="{StaticResource tb}"/>
                             < TextBox  Grid.Column ="1"  Grid.Row ="8"                            x:Name="tRegion" Style="{StaticResource tx}"
                                 Text="{Binding Path=TheRestaurant.Region, Mode=TwoWay, TargetNullValue=Beijing, ValidatesOnDataErrors=True}" IsReadOnly="{Binding IsLocked}"/>
                          
                         </ Grid >
                     </ Border >
                 </ ControlTemplate >
             </ Setter.Value >
         </ Setter >
     </ Style >
 
因为这个是自定义Dataform我们还可以使用数据绑定功能来让此DataForm作为查看每条记录的容器,只需要绑定RIA Service的数据源。
先说如何新增记录吧
需要创建一个子窗体。
 

在Home.xaml页面添加一个Add Record按钮:

 
Click事件如下:
  NewRestuarant cw = new NewRestuarant();
            CusRestuarantDataForm custDataform = new CusRestuarantDataForm();
            custDataform.Margin = new Thickness(3);
            custDataform.Width = 450;
            custDataform.TheRestaurant = new CustomRstaurant();
            custDataform.IsLocked = false;
 
            cw.LayoutRoot.Children.Add(custDataform);
            cw.HasCloseButton = false;
            cw.Title = "New Restuarant Detail";
            cw.Closed += (s, args) =>
                {
                    if (cw.DialogResult.Value && custDataform.IsValid)
                    {
                      
                    }
                };
            cw.Closing += (s, args) =>
                {
                    if (!custDataform.IsValid && cw.DialogResult.Value)
                    {
                        MessageBox.Show("Some of field values are not valid.");
                        args.Cancel = true;
                    }
                };
            cw.Show();
           运行一下,
     
   最后一步是如何使用RIA Service更新数据源了。
Closed事件发生后加入如下代码:
 
2.        使用SilverlightControlToolkit中的皮肤。
这里需要使用到ICommand方法。
先创建一个Class
public  class ThemeChangeCommand :ICommand
    {
 
        public bool CanExecute(object parameter)
        {
            return true;
        }
 
        public event EventHandler CanExecuteChanged;
 
        public void Execute(object parameter)
        {
            Theme themeContainer = (Theme)((FrameworkElement)((ContentControl)Application.Current.RootVisual).Content).FindName("ThemeContainer");
            string themeName = parameter as string;
 
            if (themeName == null)
            {
                themeContainer.ThemeUri = null;
            }
            else
            {
                themeContainer.ThemeUri =new  Uri("/System.Windows.Controls.Theming." + themeName + ";component/Theme.xaml"UriKind.RelativeOrAbsolute);
            }
 
            if (CanExecuteChanged != null)
                CanExecuteChanged(thisnew EventArgs());
        }
    }
 
第二步我们需要在APP.xaml中添加如下一句话:
    < helper : ThemeChangeCommand  x : Key ="themeCommand" />
 
最后就是修改MainPage.xaml中的Contentborder部分。
 
/uploads/allimg/100615/1100364O5-7.gif 
运行一下:
 
右键鼠标就能看到不同的皮肤了。选择个黑色的试试:
 
前提是你安装了最新的Silverlight Toolkit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值