WPF依赖属性Binding实现

由于最近一段时间一直没有做相关方面的东西,导致好多东西都忘了,就一个依赖属性绑定还倒腾了一下。特专门把相关的实现方式存留在博客园

XAML部分,其中有一大块是实现样式的,如果有需要的可以看看,其实只要把握住这么个关键点就行了,在后台定义依赖属性,xaml部分一定要记得给窗体Name属性赋值,就比如我这里给的

 x:Name="mainWindow"
再就是在binding的时候的写法
Content="{Binding ElementName=mainWindow, Path= MyContent}"
这样的话就可以大功告成了。
<Window x:Class="SpringNet.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SpringNet"
        mc:Ignorable="d"
        x:Name="mainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <ControlTemplate TargetType="{x:Type Button}" x:Key="buttonControlTemplate">
                <Border Name="RootElement">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup Name="commonStates">
                            <VisualState Name="Normal"/>
                            <VisualState Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BorderBrush"
                                                    Storyboard.TargetProperty="Color"
                                                    To="Blue"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BorderBrush"
                                                    Storyboard.TargetProperty="Color"
                                                    To="Transparent"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border.Background>
                        <SolidColorBrush x:Name="BorderBrush" Color="LightBlue"/>
                    </Border.Background>
                    <Grid Margin="4" Background="{TemplateBinding Background}">
                        <ContentPresenter
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalAlignment}"
                            />
                    </Grid>
                </Border>
            </ControlTemplate>
            <!--数据模板-->
            <DataTemplate x:Key="buttonDataTemple">

            </DataTemplate>
            <Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
                <Style.Resources>
                    <SolidColorBrush x:Key="brush" Color="Yellow"/>
                </Style.Resources>
                <Setter Property="Height" Value="25"/>
                <Setter Property="Width" Value="75"/>
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Background" Value="AliceBlue"/>
                <Setter Property="Template" Value="{StaticResource buttonControlTemplate}"/>
                <EventSetter Event="Click" Handler="btnOK_Click"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Width" Value="80"/>
                        <Setter Property="Height" Value="27"/>
                        <Setter Property="FontSize" Value="13"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Width" Value="80"/>
                            <Condition Property="Height" Value="27"/>
                        </MultiTrigger.Conditions>
                    </MultiTrigger>
                </Style.Triggers>
            </Style>
            <Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyle">
                <Setter Property="Width" Value="190"/>
                <Setter Property="Height" Value="25"/>
                <Setter Property="FontSize" Value="12"/>
            </Style>
            <Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyle">
                <Setter Property="Width" Value="190"/>
                <Setter Property="Height" Value="25"/>
                <Setter Property="BorderBrush" Value="Azure"/>
                <Setter Property="FontSize" Value="12"/>
            </Style>
        </Grid.Resources>
        <TextBlock x:Name="txtUser" 
                 Height="23"
                 Width="390"
                   Style="{StaticResource TextBlockStyle}"
                Margin="51,149,76,147"
                 />
        <Button x:Name="btnOK"
                Content="{Binding ElementName=mainWindow, Path= MyContent}"
                Style="{StaticResource ButtonStyle}"
                >
        </Button>
    </Grid>
</Window>

后台代码实现

namespace SpringNet
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        #region 字段

        /// <summary>
        /// Spring.Net
        /// </summary>
        private IApplicationContext context = ContextRegistry.GetContext();

        #endregion

        #region 构造器

        public MainWindow()
        {
            InitializeComponent();
        }

        #endregion

        #region 事件

        /// <summary>
        /// 按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            IUserB user = context.GetObject("UserB") as IUserB;
            IList<User> list = user.GetUserList();
            foreach (var each in list)
            {
                this.txtUser.Text += each.Name + ";";
            }
        }

        #endregion


        #region 依赖属性



        public string MyContent
        {
            get { return (string)GetValue(MyContentProperty); }
            set { SetValue(MyContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyContentProperty =
            DependencyProperty.Register("MyContent", typeof(string), typeof(MainWindow), new PropertyMetadata("确定"));




        public People PeopleData
        {
            get { return (People)GetValue(PeopleDataProperty); }
            set { SetValue(PeopleDataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PeopleData.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PeopleDataProperty =
            DependencyProperty.Register("PeopleData", typeof(People), typeof(MainWindow), new PropertyMetadata(null));



        public static People GetMyProperty(DependencyObject obj)
        {
            return (People)obj.GetValue(MyPropertyProperty);
        }

        public static void SetMyProperty(DependencyObject obj, int value)
        {
            obj.SetValue(MyPropertyProperty, value);
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.RegisterAttached("MyProperty", typeof(People), typeof(MainWindow), new PropertyMetadata(null));





        #endregion

    }

    public class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
    }
}

这段代码中还有有关spring.net的东西,所以可能比较杂乱。

 

posted @ 2015-02-04 17:50  古峰  阅读(2833)  评论(0编辑  收藏  举报