none
求助!关于binding有个问题请教 RRS feed

  • 问题

  • 同时给
    C# 

          Datagrid.DataContext=dt1 和 ComboBox.DataContext=dt2

    xmal:

     <DataGrid  AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="225,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="565" >
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="t_Money"   Binding="{Binding Path= t_Money,Mode= TwoWay,UpdateSourceTrigger=PropertyChanged}"  Width="100"/>
                                <DataGridTextColumn Header="t_Time" Binding="{Binding  Path=t_Time,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200"/>
                                <DataGridTextColumn Header="t_Style" Binding="{Binding  Path=t_Style,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="200"/>
                                <DataGridTextColumn Header="t_Remark" Binding="{Binding  Path=t_Remark,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="100"/>
                            </DataGrid.Columns
      </DataGrid>


     <ComboBox  SelectedIndex="0" DisplayMemberPath="Content" Height="23"  HorizontalAlignment="Left" Margin="76,45,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" /> 


    ComboBox1没有显示,datagrid1显示正常,注销任何一个DataContext,单独显示就正常,只要两个同时赋值第一个赋值的就不能显示。
    用改称itemssource 也是一样。

    datacontext 不是每个控件都有的属性吗,为什么2个控件自己找就不行呢!
    2014年8月13日 13:43

答案

  • 你好,

    我在本地创建了一个Demo来重现这个问题,我这边运行是OK的。我我想你在绑定的地方有一些问题。

    XAML:

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <DataGrid x:Name="DG" ItemsSource="{Binding Path=.}"/>
            <ComboBox Grid.Row="1" ItemsSource="{Binding Path=.}" DisplayMemberPath="Name" x:Name="CB"/>
        </Grid>

    C#:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                List<Person> personList = new List<Person>()
                {
                    new Person(){Name = "Tom", Age=20},
                    new Person(){Name = "Tom", Age=20},
                    new Person(){Name = "Tom", Age=20},
                    new Person(){Name = "Tom", Age=20},
                };
    
                List<Student> studentList = new List<Student>()
                {
                    new Student(){Name = "Tom", Age=20},
                    new Student(){Name = "Tom", Age=20},
                    new Student(){Name = "Tom", Age=20},
                    new Student(){Name = "Tom", Age=20},
                };
    
                DG.DataContext = personList;
                CB.DataContext = studentList;
            }
        }
    
        public class Person
        {
            public int Age { get; set; }
            public string Name { get; set; }
        }
    
        public class Student
        {
            public int Age { get; set; }
            public string Name { get; set; }
        }

    截图:

    谢谢!


    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年8月14日 6:00
    版主
  • 您好,谢谢您的回复。我试过了List或者ObservableCollection 都没有问题,唯独DataTable有这个问题,后来我用ObservableColletion,因为它有PropertyChanged。
    2014年8月21日 3:20

全部回复

  • 你好,

    我在本地创建了一个Demo来重现这个问题,我这边运行是OK的。我我想你在绑定的地方有一些问题。

    XAML:

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <DataGrid x:Name="DG" ItemsSource="{Binding Path=.}"/>
            <ComboBox Grid.Row="1" ItemsSource="{Binding Path=.}" DisplayMemberPath="Name" x:Name="CB"/>
        </Grid>

    C#:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                List<Person> personList = new List<Person>()
                {
                    new Person(){Name = "Tom", Age=20},
                    new Person(){Name = "Tom", Age=20},
                    new Person(){Name = "Tom", Age=20},
                    new Person(){Name = "Tom", Age=20},
                };
    
                List<Student> studentList = new List<Student>()
                {
                    new Student(){Name = "Tom", Age=20},
                    new Student(){Name = "Tom", Age=20},
                    new Student(){Name = "Tom", Age=20},
                    new Student(){Name = "Tom", Age=20},
                };
    
                DG.DataContext = personList;
                CB.DataContext = studentList;
            }
        }
    
        public class Person
        {
            public int Age { get; set; }
            public string Name { get; set; }
        }
    
        public class Student
        {
            public int Age { get; set; }
            public string Name { get; set; }
        }

    截图:

    谢谢!


    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年8月14日 6:00
    版主
  • 您好,谢谢您的回复。我试过了List或者ObservableCollection 都没有问题,唯独DataTable有这个问题,后来我用ObservableColletion,因为它有PropertyChanged。
    2014年8月21日 3:20
  • 谢谢您耐心的回复 。可能是我表达的不清楚,我是在从数据库里读出2个表格数据分别存到 dt1和dt2里 。分别绑定给 comboBox1和 dataGrid1里绑定方法跟您写的一样。只是只能是第二赋值能显示出来(也就是dataGrid1可以正常显示)comboBox1显示不了

    DataTable dt1 = new DataTable();

    DataTable dt2 = new DataTable();

     oleDbDataAdapter = new OleDbDataAdapter(sqlSentence, oleDbConnection);
     oleDbDataAdapter.Fill(dt1);

    oleDbDataAdapter = new OleDbDataAdapter(sqlSentence, oleDbConnection);
     oleDbDataAdapter.Fill(dt2);

    comboBox1.ItemSource=dt1;

    dataGrid1.ItemSource=dt2;

    <DataGrid x:Name="DG" ItemsSource="{Binding Path=.}"/>
     
    <ComboBox Grid.Row="1" ItemsSource="{Binding Path=.}" DisplayMemberPath="Name" x:Name="CB"/>

    2014年8月26日 14:52