none
wpf 用户控件自定义依赖属性 绑定 RRS feed

  • 问题

  • 我做一个用户控件,我在控件上用了一个 依赖属性,让外面使用的时候在 xaml 中可以直接banding 。但是现在不行。  

    这个是 window 中banding 代码。  

    <Window.DataContext >
            <self:TempRadarData></self:TempRadarData>
        </Window.DataContext>
        <Grid >
            <!--<RadarMapCommon:RadarMapMain ItemsSourceData="{Binding ItemsSources}" />-->
            <RadarMapControl:RadarMap x:Name="radar" RadarMapName="{Binding Name}"  ItemsSourceData="{Binding ItemsSources}"/>

    这个是用户控件的依赖属性代码

     public static readonly DependencyProperty ItemsSourceDataProperty =
                DependencyProperty.Register("ItemsSourceData", typeof(ObservableCollection<Member>), typeof(RadarMap), new PropertyMetadata(new PropertyChangedCallback(ItemsSourceDataChanged)));

            public ObservableCollection<Member> ItemsSourceData 
            {
                get { return (ObservableCollection<Member>)GetValue(ItemsSourceDataProperty); }
                set { SetValue(ItemsSourceDataProperty, value); }
            }
            private static void ItemsSourceDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if (e.NewValue != null)
                {
                    RadarMap radar = d as RadarMap;
                    //radar.ItemsSourceData = e.NewValue as ObservableCollection<Member>;
                    radar.RefreshMembers(e.NewValue as ObservableCollection<Member>);
                }
            }
            private void RefreshMembers(ObservableCollection<Member> members)
            {
                this.Members.ItemsSource = members;
            }
    2014年6月3日 7:43

答案

  • 你好,

    如果你想要创建一个依赖属性用于绑定数据源,需要实现IEnumerable接口,这里我用一个示例向你展示如何实现一个类似功能:

    UserControl1后台:

    public partial class UserControl1 : UserControl
    {
            public UserControl1()
            {
                InitializeComponent();
            }
    
            public static readonly DependencyProperty ItemsSourceDataProperty;
    
            static UserControl1()
            {
                UserControl1.ItemsSourceDataProperty =
                                           DependencyProperty.Register("ItemsSourceData",
                                           typeof(IEnumerable), typeof(UserControl1));
            }
    
            public IEnumerable ItemsSourceData
            {
                get
                {
                    return (IEnumerable)GetValue(UserControl1.ItemsSourceDataProperty);
                }
                set
                {
                    SetValue(UserControl1.ItemsSourceDataProperty, value);
                }
            }
    }

    UserControl1中有一个ComboBox控件,ItemsSource和ItemsSourceData绑定:

    Name="myUserControl">
        <Grid>
            <ComboBox Height="25" Width="100" Name="cbo" ItemsSource="{Binding ElementName=myUserControl, Path=ItemsSourceData}"/>
        </Grid>

    TempRadarData VM:

    public class TempRadarData : INotifyPropertyChanged
    {
            public ObservableCollection<string> ItemsSources{ get; set; }
            public TempRadarData()
            {
                ItemsSources = new ObservableCollection<string>() { "Item1", "Item2", "Item3", "Item4" };
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
    }

    MainWindow:

    <Window.DataContext>
            <local:TempRadarData />
        </Window.DataContext>
        <Grid>
            <local:UserControl1 x:Name="uc1" ItemsSourceData="{Binding ItemsSources, UpdateSourceTrigger=PropertyChanged}" />
        </Grid>

    截图:

    这样,可以使用ItemsSourceData对用户控件的数据源进行绑定。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月4日 7:52
    版主

全部回复

  • 你好,

    如果你想要创建一个依赖属性用于绑定数据源,需要实现IEnumerable接口,这里我用一个示例向你展示如何实现一个类似功能:

    UserControl1后台:

    public partial class UserControl1 : UserControl
    {
            public UserControl1()
            {
                InitializeComponent();
            }
    
            public static readonly DependencyProperty ItemsSourceDataProperty;
    
            static UserControl1()
            {
                UserControl1.ItemsSourceDataProperty =
                                           DependencyProperty.Register("ItemsSourceData",
                                           typeof(IEnumerable), typeof(UserControl1));
            }
    
            public IEnumerable ItemsSourceData
            {
                get
                {
                    return (IEnumerable)GetValue(UserControl1.ItemsSourceDataProperty);
                }
                set
                {
                    SetValue(UserControl1.ItemsSourceDataProperty, value);
                }
            }
    }

    UserControl1中有一个ComboBox控件,ItemsSource和ItemsSourceData绑定:

    Name="myUserControl">
        <Grid>
            <ComboBox Height="25" Width="100" Name="cbo" ItemsSource="{Binding ElementName=myUserControl, Path=ItemsSourceData}"/>
        </Grid>

    TempRadarData VM:

    public class TempRadarData : INotifyPropertyChanged
    {
            public ObservableCollection<string> ItemsSources{ get; set; }
            public TempRadarData()
            {
                ItemsSources = new ObservableCollection<string>() { "Item1", "Item2", "Item3", "Item4" };
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
    }

    MainWindow:

    <Window.DataContext>
            <local:TempRadarData />
        </Window.DataContext>
        <Grid>
            <local:UserControl1 x:Name="uc1" ItemsSourceData="{Binding ItemsSources, UpdateSourceTrigger=PropertyChanged}" />
        </Grid>

    截图:

    这样,可以使用ItemsSourceData对用户控件的数据源进行绑定。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月4日 7:52
    版主
  • 你好,谢谢支持,我把  ObservableCollection 改成 IEnumerable 也是不行,绑定过去没有反应。我真不知道问题出在哪里。

    2014年6月9日 2:29
  • 你好,谢谢支持,我把  ObservableCollection 改成 IEnumerable 也是不行,绑定过去没有反应。我真不知道问题出在哪里。

    你好,

    根据你的描述,TempRadarData应该是你的ViewModel, 请展示下TempRadarData中的实现。

    还有,直接把 ObservableCollection 改成 IEnumerable 是不对的,请依照我的示例工程,根据你的需求再次修改。



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月9日 6:50
    版主