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的東西,所以可能比較雜亂。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM