WPF DataGrid 绑定不起作用 caliburn micro

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15601278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:17:27  来源:igfitidea点击:

WPF DataGrid binding don't work caliburn micro

wpfbindingdatagridcaliburn.microcaliburn

提问by puti26

I can't understand why it does not work..

我不明白为什么它不起作用..

This is my class

这是我的课

public class Article : Screen
{
    public string Code { get; set; }
    public string Description{ get; set; }
    public decimal Cost{ get; set; }
    public decimal Price{ get; set; }
}

This is XAML code of DataGrid:

这是 DataGrid 的 XAML 代码:

    <DataGrid Height="211" HorizontalAlignment="Left" 
        Margin="12,31,0,0" VerticalAlignment="Top" Width="521" 
        AutoGenerateColumns="False" ItemsSource="{Binding List}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Code}" Header="Code" />
            <DataGridTextColumn Binding="{Binding Path=Description}" Header="Description" />
            <DataGridTextColumn Binding="{Binding Path=Cost}" Header="Cost" />
            <DataGridTextColumn Binding="{Binding Path=Price}" Header="Price" />
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Button" Height="39" HorizontalAlignment="Left" 
            Margin="223,262,0,0" VerticalAlignment="Top" Width="110"
            x:Name="AllArticles"/>

And this is my viewmodel

这是我的视图模型

[Export(typeof(IShell))]
public class ArtsViewModel : Screen
{
    public List<Article> List = new List<Article>();

    public void AllArticles()
    {
        Recover recover = new Recover();  //a model called Recover
        List = recover.Import().Articles; //return a List of Article
        NotifyOfPropertyChange("List");
    }    
}

WHY THE DATAGRID DON'T WORK ?

为什么数据网格不起作用?

回答by Anders Gustafsson

To enable the binding conventions in Caliburn.Micro, you normally use the x:Nameproperty in your XAML. If you, instead of explicitly binding the Listproperty to ItemsSourceof your DataGrid, use the name convention like this:

要在Caliburn.Micro 中启用绑定约定,您通常x:Name在 XAML 中使用该属性。如果是你,而不是明确地结合的List财产ItemsSource你的DataGrid,使用的名称约定如下:

<DataGrid x:Name="List" Height="211" HorizontalAlignment="Left" 
    Margin="12,31,0,0" VerticalAlignment="Top" Width="521" 
    AutoGenerateColumns="False">

I believe the subsequent bindings should work as desired.

我相信后续的绑定应该可以正常工作。

Oh, and you also need to make Lista property instead of a field:

哦,你还需要创建List一个属性而不是一个字段:

public List<Article> List { get; private set; }

If you want to make sure that modifications to Listare properly reflected in your data grid, you should also make your Listproperty an IObservableCollectionwith a backing field:

如果您想确保修改List正确反映在您的数据网格中,您还应该使您的List属性IObservableCollection具有支持字段:

private IObservableCollection<Article> _list;
public IObservableCollection<Article> List { 
    get { return _list; }
    set {
        _list = value;
        NotifyOfPropertyChange(() => List);
    }
}

回答by kollmanns

a) Did you set the DataContext?

a) 您是否设置了 DataContext?

b) Try "Binding="{Binding Code}" (Without Path=), works fine for me.

b) 尝试“Binding="{Binding Code}”(没有 Path=),对我来说效果很好。

回答by N Cheadle

For my UWP app I found that the x:Name convention didn't work and I need to use a bindable collection and bind directly to ItemsSource. I then used Path=PropertyName to specify the columns.

对于我的 UWP 应用程序,我发现 x:Name 约定不起作用,我需要使用可绑定集合并直接绑定到 ItemsSource。然后我使用 Path=PropertyName 来指定列。

XAML:

XAML:

<controls:DataGrid Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3"  ItemsSource="{Binding RawCSVData}" AutoGenerateColumns="false">
        <controls:DataGrid.Columns>
            <controls:DataGridTextColumn Header="Time" Binding="{Binding Path=Time}"/>
            <controls:DataGridTextColumn Header="Signal" Binding="{Binding Path=Signal}"/>
            <controls:DataGridTextColumn Header="Response" Binding="{Binding Path=Response}"/>

        </controls:DataGrid.Columns>
    </controls:DataGrid>

CSVData.cs:

CSVData.cs:

public class CSVData: Screen
{
    public double Time { get; set; }
    public double Signal { get; set; }
    public double Response { get; set; }
}

ViewModel.cs snippet:

ViewModel.cs 片段:

public BindableCollection<CSVData> RawCSVData { get; set; }

回答by kuberzzz333

     private void _bookeditpage_ConfirmBook(Book _book, bool _isnewone) 
     {
        if (_isnewone)
        {
            Books.Add(_book);
        }
        else
        {
            //doesn't refresh datagrid
            SelectedBook = _book;
            //

            //works fine :)

            SelectedBook.BookName = _book.BookName;
            SelectedBook.IsPrenum=_book.IsPrenum;
            SelectedBook.Publisher=_book.Publisher;

           //
        }
    }