zoukankan      html  css  js  c++  java
  • [转]Using Bindings in Styles in Silverlight

    This document comes from this blog. I think it's very useful and reposted here.

    http://blogs.profitbase.com/tsenn/?p=53

    One of the differences between WPF and Silverlight is that using Bindings when setting properties in styles is still not supported (SL 4). If you try to do so, your application will simply crash.
    In WPF, you can do something like this;

    <Style TargetType="{x:Type ListBoxItem}">
           <Setter Property="Background" Value="{Binding Path=MyColor}"/>
    </Style>

    Although not directly supported, you can achieve the same thing in Silverlight using attached properties. The workaround is to specify an Attached Property in the Setter Property, and then use a “proxy” in the Setter Value to set up the binding between the actual property of the UI element and the object from the data binding. This technique can also be used for things like wiring up command to react to user input, for example when a user double clicks a ListBoxItem.



    The following example shows how to set the background color of a ListBoxItem through data binding, and also how to react to a double click on the item.

    <ListBox x:Name="listBox1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="b:BindingBehavior.PropertyBinding">
                            <Setter.Value>
                                <b:PropertyBinding Binding="{Binding Path=BackgroundBrush}" Property="Background"/>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="b:BindingBehavior.DoubleClickCommandBinding">
                            <Setter.Value>
                                <b:CommandBinding Command="{Binding Path=DataContext.TestCommand, ElementName=LayoutRoot}"
                                                  CommandParameter="{Binding}"/>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
    

    As you can see, both the Setter Properties have Attached Properties defined instead of the actual property names of the ListBoxItem. In the Setter Values, I have added my proxy classes where I have specified the bindings and the name of the ListBoxItem property I want a binding to apply to.

    The BindingProperty proxy class is very simple and contains only two properties; one for holding the name of the property you want to bind to and one for the binding itself.

    public class PropertyBinding
        {
            public Binding Binding { get; set; }
            public string Property { get; set; }
        }

    The BindingBehavior class is responsible for creating the actual binding based on an instance of the PropertyBinding. It does so using reflection on the ListBoxItem to locate the dependency property specified in the Property attribute of the PropertyBinding proxy instance.

    private static void OnPropertyBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                FrameworkElement frameworkElement = d as FrameworkElement;
                if (frameworkElement == null)
                {
                    throw new ArgumentException("The PropertyBinding behavior can only be applied to FrameworkElement types");
                }
    
                PropertyBinding styleBinding = e.NewValue as PropertyBinding;
                if (styleBinding != null)
                {
                    string depPropName = string.Concat(styleBinding.Property, "Property");
                    if (depPropName.IndexOf('.') > -1)
                    {
                        int index = depPropName.LastIndexOf('.');
                        depPropName = depPropName.Substring(index);
                    }
                    FieldInfo dependencyPropertyField = frameworkElement.GetType().GetField(depPropName, BindingFlags.Public
                        | BindingFlags.Static
                        | BindingFlags.FlattenHierarchy);
                    if (dependencyPropertyField != null)
                    {
                        DependencyProperty dependencyProperty = dependencyPropertyField.GetValue(null) as DependencyProperty;
                        if (dependencyProperty != null)
                        {
                            frameworkElement.SetBinding(dependencyProperty, styleBinding.Binding);
                        }
                    }
                }
    
            }
     
    For the command binding, a similar approach is used. The CommandBinding proxy class contains a Binding property for setting the Command and CommandParameter, and then the BindingBehavior class creates the actual binding in the OnXXXChanged event handler.
    
    
    public class CommandBinding
        {
            public Binding Command { get; set; }
            public Binding CommandParameter { get; set; }
        }
    private static void OnDoubleClickCommandBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                FrameworkElement element = d as FrameworkElement;
                if (element == null)
                {
                    throw new InvalidOperationException("This behavior can only be applied to FrameworkElement types");
                }
    
                CommandBinding styleBinding = e.NewValue as CommandBinding;
                if (styleBinding != null)
                {
                    element.SetBinding(BindingBehavior.DoubleClickProperty, styleBinding.Command);
                    if (styleBinding.CommandParameter != null)
                    {
                        element.SetBinding(BindingBehavior.DoubleClickParameterProperty, styleBinding.CommandParameter);
                    }
                    else
                    {
                        element.ClearValue(BindingBehavior.DoubleClickParameterProperty);
                    }
                }
                else
                {
                    element.ClearValue(BindingBehavior.DoubleClickProperty);
                    element.ClearValue(BindingBehavior.DoubleClickParameterProperty);
                }
            }
  • 相关阅读:
    Oracle 的merge into 用法
    个人博客作业——结课总结
    个人博客作业week7
    结对项目总结博客
    #个人博客作业week3——微软必应词典的使用
    #个人博客作业week2——结对编程伙伴代码复审
    #个人博客作业week2——关于代码规范的个人观点
    #个人博客作业——目前流行的源程序版本管理软件和项目管理软件优缺点
    个人项目——四则运算题目的随机生成
    #个人博客作业Week1——浏览教材后提出的六个问题及软件与软件工程的提出。
  • 原文地址:https://www.cnblogs.com/JerryWeng/p/1964893.html
Copyright © 2011-2022 走看看