locked
怎么作控件互动的效果? RRS feed

  • 问题

  • 我有一个datagrid,一个文本框。

    我想点击修改datagrid cell的同时,文本框显示修改的文本,该怎么做呢?

    例如,我双击 datagrid cell,修改值从 100 到200,文本框也会同步显示 100 到200。

    谢谢
    2009年8月21日 20:28

答案

  • 你好,

    下面是代码. TextBlock绑到第一个对象:
      public MainPage()
            {
                InitializeComponent();
                for (int i = 0; i < 10; i++)
                {

                    Customer c = new Customer { age = i };
                    array.Add(c);
                }

                this.DataGrid1.ItemsSource = array;
                this.testTextBlock.DataContext = array[0];
        
            }


     public class Customer : INotifyPropertyChanged
        {


            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            private int _age;

            public int age
            {
                get { return _age; }
                set
                {
                    _age = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("age"));
                    }
                }
            }

            #endregion
        }


       <TextBlock x:Name="testTextBlock" Text="{Binding age}"></TextBlock>
                <data:DataGrid AutoGenerateColumns="False"  Width="500" Height="500" x:Name="DataGrid1">
                    <data:DataGrid.Columns>
                        <data:DataGridTemplateColumn Width="80">
                            <data:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding age}"></TextBlock>
                                </DataTemplate>
                            </data:DataGridTemplateColumn.CellTemplate>
                            <data:DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding age, Mode=TwoWay}"></TextBox>
                                </DataTemplate>
                            </data:DataGridTemplateColumn.CellEditingTemplate>
                        </data:DataGridTemplateColumn>

                    </data:DataGrid.Columns>
                </data:DataGrid>


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Need a sample of a technique of Microsoft? Just check out CodeFx first! http://cfx.codeplex.com/
    2009年8月24日 7:21

全部回复

  • 你好,

    下面是代码. TextBlock绑到第一个对象:
      public MainPage()
            {
                InitializeComponent();
                for (int i = 0; i < 10; i++)
                {

                    Customer c = new Customer { age = i };
                    array.Add(c);
                }

                this.DataGrid1.ItemsSource = array;
                this.testTextBlock.DataContext = array[0];
        
            }


     public class Customer : INotifyPropertyChanged
        {


            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            private int _age;

            public int age
            {
                get { return _age; }
                set
                {
                    _age = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("age"));
                    }
                }
            }

            #endregion
        }


       <TextBlock x:Name="testTextBlock" Text="{Binding age}"></TextBlock>
                <data:DataGrid AutoGenerateColumns="False"  Width="500" Height="500" x:Name="DataGrid1">
                    <data:DataGrid.Columns>
                        <data:DataGridTemplateColumn Width="80">
                            <data:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding age}"></TextBlock>
                                </DataTemplate>
                            </data:DataGridTemplateColumn.CellTemplate>
                            <data:DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding age, Mode=TwoWay}"></TextBox>
                                </DataTemplate>
                            </data:DataGridTemplateColumn.CellEditingTemplate>
                        </data:DataGridTemplateColumn>

                    </data:DataGrid.Columns>
                </data:DataGrid>


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Need a sample of a technique of Microsoft? Just check out CodeFx first! http://cfx.codeplex.com/
    2009年8月24日 7:21
  • 谢谢答复 。

    请问,因为我的data是动态的,请问如何使用behind code进行binding或者edit呢?
    2009年8月25日 20:24
  • 你好,
    请问你说的动态具体是什么意思?能够详细说明一下吗?
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Need a sample of a technique of Microsoft? Just check out CodeFx first! http://cfx.codeplex.com/
    2009年8月26日 1:37
  • 请问可以使用Element Binding实现以上功能么?
    2009年8月27日 15:44