WPF中DataGrid更改后台数据并更新前台显示的问题

在做项目的时候是用WPF做界面的,其中涉及到了用DataGrid显示数据的功能,要求当在后台对源数据进行修改时,前台的数据显示能自动更新,因为是第一次接触WPF,所以不太会,网上查了很多,都是前台更改数据更新到源数据的,跟我的目标正好相反,自己摸索查资料终于解决了,现在写出来造福大众。

前台:

<DataGrid x:Name="dataGrid" ItemsSource="{Binding}" AutoGenerateColumns="false" SelectionUnit="CellOrRowHeader" SelectionMode="Single" IsReadOnly="True" Width="320" HorizontalAlignment="Left" Margin="10,20,0,415">
    <DataGrid.Columns>
        <DataGridTextColumn Header="小车编号" Width="80" Binding="{Binding Path=CarId,Mode=OneWay}"/>
        <DataGridTextColumn Header="状态" Width="50" Binding="{Binding Path=CarState,Mode=OneWay}" />
        <DataGridTextColumn Header="X坐标" Width="60" Binding="{Binding Path=CarX,Mode=OneWay}"/>
        <DataGridTextColumn Header="Y坐标" Width="60" Binding="{Binding Path=CarY,Mode=OneWay}"/>
        <DataGridTextColumn Header="角度" Width="61" Binding="{Binding Path=CarAngle,Mode=OneWay}"/>
   </DataGrid.Columns>
</DataGrid>

后台:

 public MainWindow()
        {
            InitializeComponent();
            //coll = comboBoxCar.Items;
           memberData = new ObservableCollection<Member>();
            memberData.Add(new Member()
            {
                CarId = "0",
                CarState = "故障",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "1",
                CarState = "故障",
                CarX="0",
                CarY="0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "2",
                CarState = "正常",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "3",
                CarState = "故障",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "4",
                CarState = "正常",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "5",
                CarState = "正常",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "6",
                CarState = "正常",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "7",
                CarState = "正常",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "8",
                CarState = "正常",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            memberData.Add(new Member()
            {
                CarId = "9",
                CarState = "正常",
                CarX = "0",
                CarY = "0",
                CarAngle = "0º",
            });
            dataGrid.DataContext = memberData;

运行结果:


现在我添加一个按钮,一旦点击,就修改后台的数据,并实现前台的更新。

后台代码:

private void Button_Click(object sender, RoutedEventArgs e)
        {
            string X = "50;2;65;23;12;;34;;0;80";
            string[] xs = X.Split(new Char[]{';'},StringSplitOptions.None);
            int i = 0;
            foreach (Member item in memberData)
            {
               
                item.CarX = xs[i++];
                dataGrid.ItemsSource = null;//这里是关键
                dataGrid.ItemsSource = memberData;
            }
        }

效果:


修改成功!

版权声明:本文为博主原创文章,未经博主允许不得转载。 

http://blog.csdn.net/qq_31494787/article/details/79582165

展开阅读全文

没有更多推荐了,返回首页