none
DataGridTemplateColumn.CellTemplate中的自定义控件不能排序? RRS feed

  • 问题

  • 下列DataGrid中的三列,当点击"序号"排序时,“列2”会跟着排序, 而自定义控件“列1”却不会跟着排序(保持原样), 什么原因尼?


        <sdk:DataGridTextColumn Header="序号" Binding="{Binding xx}" IsReadOnly="True"/>

        <sdk:DataGridTemplateColumn Header="列1">
         <sdk:DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
                    <XmglBase:MyMedia myText="{Binding word}" Margin="4,0,4,0"/>
          </DataTemplate>
         </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>

        <sdk:DataGridTemplateColumn Header="列2">
         <sdk:DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
                   <TextBlock FontSize="12" Margin="4,4,4,4" Text="{Binding word}"/>
          </DataTemplate>
         </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
        <sdk:DataGridTemplateColumn.CellEditingTemplate>
     <DataTemplate>
                    <XmglBase:MyMedia myText="{Binding word}" Margin="4,0,4,0"/>
     </DataTemplate>
        </sdk:DataGridTemplateColumn.CellEditingTemplate>

    其中XmglBase:MyMedia 大致如此:
    namespace XmglBase
    {
        public partial class MyMedia : UserControl
       {

            public MyMedia()
           {
                InitializeComponent();
                this.Loaded += My_Loaded;
            }
            void My_Loaded(object sender, RoutedEventArgs e)
            {
               this.DispText.Text = myText;
            }

            public static readonly DependencyProperty myTextProperty = DependencyProperty.Register("myText", typeof(String), typeof(MyMedia), null);
            public String myText
            {
                get
                {
                    return (String)GetValue(myTextProperty);
                }
                set
                {
                    SetValue(myTextProperty, value);
                }
            }

    }

    2015年1月24日 8:22

全部回复

  • Hi,

    When you set the columns manually you need to specify the SortMemberPath property for each of the columns you want to sort/group by.

    The SortMemberPath specifies the name/path of the property you whish to sort by when the user clicks on that column header, in most cases it's the same path specified for the Binding.

    More information,please check this:

    http://blogs.msdn.com/b/scmorris/archive/2008/06/10/sorting-data-in-the-silverlight-datagrid.aspx


    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.




    2015年1月26日 2:10
  • 谢谢你的回复

    但也许我说得不够清楚:你回答的是如何使用SortMemberPath来制定一列排序的排序规则,但我的问题并不是这个。

    我重新来叙述一遍:

    当我点击“序号”列时,“序号”列发生了排序,按道理“列1”也会跟着排序,但现在“列1”却并没有发生变化。

    原因是我的“列1”是个自定义控件, 我分析原因,是这个自定义控件MyMedia只在构造函数里刷新了介面,在后来的排序时未能刷新介面。我不知道错在了那里,请大神指教

    在这个自定义控件MyMedia中,MyText绑定了字段word,  这个word是这样定义的:

    public class Xm_struct : System.ComponentModel.INotifyPropertyChanged
        {
            public string word
                        {

                           get { if (this.S117w == null) return ""; else return this.S117w; }

                            set { this.S117w = value; OnPropertyChanged("word"); }

                        }       

            private string S117w;

            #region INotifyPropertyChanged Members
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                System.ComponentModel.PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion

        }

    2015年1月26日 11:51