WPF 笔记 一

数据绑定

可以自定义数据类, 把类实例通过 Text={Binding path="propertyName"}绑定到控件上. 

自定义数据类有个问题, 就是修改类实例不能自动刷新到控件上. ExpandoObject 实例有这个功能, 那是因为它实现了 INotifyPropertyChanged接口. 


数据绑定可以分为对象绑定和对象集合绑定. 对象绑定通常用DataContext, path 即可, 而对象集合则可以使用ItemsSource, DisplayMemberPath, ItemTemplate 来实现. 


布局

控件布局可以通过 HorizontalAlignment, VerticalAlignment, Margin, Width, Height 完全定义出来, 任何位置都可以精确的使用这些属性精确定位. 


任何和数据相关的东西都需要和Binding对象关联起来:
Source = "{Binding Path=}"
ItemsSource="{Binding Source={StaticResource ResourceKey=cvs}}"
Content="{Binding Path=Quantity}">

Text="{Binding ElementName=sldierFontSize, Path = Value}"

Command="{Binding SortCommand}";

<TextBlock.Text>
    <Binding Path="Title">
        <Binding.RelativeSource>
            <RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window}"/>
            </Binding.RelativeSource>
        </Binding>
    </TextBlock.Text>
</TextBlock>

当然更常用的是标记扩展: 


Text="{Binding Path=Title, RelativeSource = { RelativeSource FindAncestor, AncestorType={x:Type Window}}}";


查找界面上所有控件的方法:

public static class Utility
    {
        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }
    }

尽量不要用这个方法, 控件和数据操作应该要分开.MVVM模式的设计初衷既是为此. 应该操作数据而不应该查找控件来修改控件属性.


Validation.ErrorTemplate+ ValidationRule  可以完成验证和错误信息显示的所有工作.


标记扩展和xml模式的区别: 

通常在标记扩展中 "{Binding Source=}", 这样的'Source='字符表示定义一个Bindiing对象的Source属性. 在xml中表示为:  <Binding.Source></Binding.Souce>更能说明Sourcez是Binding对象的一个属性. 另外有  RelativeSource = { RelativeSource FindAncestor, AncestorType={x:Type Window}} 这里 RelativeSource FindAncestor没有=说明 FindAncesotr是一个RelativeSource实例. 


x:  命名空间

x:key 以命名空间前缀x:开头, 会将该对象放置到XMAL命名空间而不是WPF命名空间.


资源

每个元素都有 Resources属性, 他是一个资源字典集合. 资源可以包含任意类型的对象. 

可以在资源部分中实例化任何.NET类, 只要该类是XAML友好的, 这意味着该类要提供一些基本特性,包括无参构造函数和可写的属性. 

静态资源和动态资源: 

1. 静态资源在程序加载时就实例化, 动态资源会重新应用资源如果资源发生改变. 

2. 使用静态资源必须在使用资源之前的表记中定义该资源. 而动态资源无此限制,但是开销会大一些. 

3.  静态资源和动态资源可能指向的是同一个对象,只不过使用时一个指定为动态一个指定为静态: <Button name="btn1" Background="{DynamicResource TitleBrush}" /> 

<Button name="btn2" Background="{StaticResource TitleBrush}"/>

4. 在代码中直接对资源对象进行修改时, 不管是静态还是动态资源都会实时的修改界面. 但如果只是修改引用资源的对象的Resources属性时, 静态资源不会实时修改界面而动态资源会.  

//直接修改资源对象,不管是静态还是动态资源都会修改界面
ImageBrush brush = (ImageBrush) this.Resources["TitleBrush"];
brush.Viewport= new Rect(0,0,5,5);

//只修改对资源的引用,则静态资源不会实时修改界面
this.Resources["TitleBrush"] =new SolidColorBrush(Colors.LightBlue);

原因在于动态资源会实时的获取资源对象, 而静态资源只在加载的时候获取一次对象. 

5. 使用动态资源的一般情况:

依赖于系统设置属性, 系统属性可能会在程序执行时被修改

有可能要在代码中动态的更换资源对象. 


6. 反射获取对象属性值

var propertyValue= (typeof(T).GetProperty(fieldName).GetValue(obj, null);


7. 抽象类和接口

8. ContentRendered 事件和 Loaded 事件:Loaded is raised just before this window is shown. ContentRendered is raised when the window is visually rendered.  Loaded event is raised first. Windows shows before all items in window are rendered.

     

Open:

    1. SourceInitiated
    2. Activated
    3. Loaded
    4. ContentRendered

Close:

    1. Closing
    2. Deactivated
    3. Closed




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值