您的位置:首页 > 其它

WPF 数据绑定需要注意的地方

2014-02-17 18:08 267 查看
在学习WPF过程中发现的一些需要注意的地方,在此记录,以备后查。

14.2.17

FrameworkElement,控件的常用方法:

1. public void SetValue(DependencyProperty dp, object value);设置XAML文件中WindowsSource中的一个资源,object value为Key的名称而非Name.

2. public void SetResourceReference(DependencyProperty dp, object name);设置某个属性值

3. 设置属性的方式与绑定效果相同:

textblockNo.Content = source;

Binding bind = new Binding("Key");

control.SetBinding(TextBlock.TextProperty,binding);

----------------------------------------------------------------------------

Binding bind = new Binding("Key");

binding.Source = source;

control.SetBinding(TextBlock.TextProperty,binding);

有人知道原因吗?.

4. 一个简单转换器的写法:

public class DateConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

        {

            if (value.ToString().IndexOf("+") >= 0

                || value.ToString().IndexOf("[") >= 0)

                return value;

            else return "+" + value;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

        {

            return value;

        }

    }

5. 简单样式的写法,写在<Window.Resources/>中

  <Style x:Key="S_TextBlock_No" TargetType="TextBlock">

            <Setter Property="Foreground"  Value="Blue"/>

            <Setter Property="FontFamily" Value="微软雅黑"/>

            <Setter Property="FontSize" Value="13"/>

            <Setter Property="Margin" Value="0,5,0,0"/>

            <Setter Property="HorizontalAlignment" Value="Center"/>

        </Style>

6. XAML中绑定Window.Recources的方法

<TextBlock x:Name="tb_sampleCode1_2" Style="{DynamicResource S_TextBlock_Content}"/>:

后台等同于

tb_sampleCode1_2.SetResourceReference(TextBlock.StyleProperty, "S_TextBlock_Content");

7. Block中只能加一个下级,因此可以通过增加一个容器来扩展.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: