简体   繁体   English

Caliburn.Micro MVVM WPF - 在用户编辑 DataGrid 时启用按钮,否则禁用它

[英]Caliburn.Micro MVVM WPF - enable a button when DataGrid has been edited by the user, otherwise disable it

I am new to MVVM and Caliburn.Micro.我是 MVVM 和 Caliburn.Micro 的新手。

I have implemented a DataGrid that is binded to <BindableCollection><CustomerModel> where CustomerModel is:我已经实现了一个绑定到<BindableCollection><CustomerModel>的 DataGrid,其中 CustomerModel 是:

    public class CustomerModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string StreetName { get; set; }
    public int HouseNumber { get; set; }
    public string ApartmentNumber { get; set; }
    public string PostalCode { get; set; }
    public string Town { get; set; }
    public int PhoneNumber { get; set; }
    public string DateOfBirth { get; set; }
    public int Age { get; set; }
}

This data is loaded from XML file on startup and also can be saved into an XML file by clicking on a button "Save" which is connected to method SaveToXML() and obviously to CanSaveToXML().此数据在启动时从 XML 文件加载,也可以通过单击连接到方法 SaveToXML() 和 CanSaveToXML() 的按钮“保存”保存到 XML 文件中。 Saving and loading works perfectly but I want to make the "Save" button enabled only if the user has edited the DataGrid, otherwise it should stay disabled.保存和加载工作完美,但我只想在用户编辑 DataGrid 时启用“保存”按钮,否则它应该保持禁用状态。

I have tried comparing the current number of objects of CustomerModel class to the number of objects of CustomerModel at startup.我尝试将 CustomerModel class 的当前对象数与启动时 CustomerModel 的对象数进行比较。 If the number is different, it should enable the button as it indicates that someone has entered or deleted an entry.如果数字不同,它应该启用按钮,因为它表示有人输入或删除了一个条目。

        public bool CanSaveToXML()
    {
        if (CustomersOnStartup != Customers.Count())
            return true;
        else
            return false;
    }

where在哪里

public BindableCollection<CustomerModel> Customers { get; set; }

The problem is that the Customers.Count() is executed only once and the number doesn't update after the startup and adding/deleting an entry by the user.问题是 Customers.Count() 只执行一次,并且在启动和用户添加/删除条目后数字不会更新。 I know that it is probably connected to我知道它可能与

NotifyOfPropertyChange(() => ???)

but I have no idea how to use it in relation to number of objects with Customers.Count()但我不知道如何使用它与 Customers.Count() 的对象数量相关

Any ideas are welcome.欢迎任何想法。

XAML implemenetation: XAML 实现:

    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="20"/>

    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="20"/>
    </Grid.RowDefinitions>
    <DataGrid Grid.Row="1" Grid.Column="1" x:Name="Customers" AlternatingRowBackground="LightGray"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" Width="auto" Binding="{Binding Path=FirstName, Mode=TwoWay}" />
            <DataGridTextColumn Header="Last Name" Width="auto" Binding="{Binding Path=LastName}" />
            <DataGridTextColumn Header="Street Name" Width="auto" Binding="{Binding Path=StreetName}" />
            <DataGridTextColumn Header="House Number" Width="auto" Binding="{Binding Path=HouseNumber}" />
            <DataGridTextColumn Header="Apartment Number" Width="auto" Binding="{Binding Path=ApartmentNumber}" />
            <DataGridTextColumn Header="Postal Code" Width="auto" Binding="{Binding Path=PostalCode}" />
            <DataGridTextColumn Header="Town" Width="auto" Binding="{Binding Path=Town}" />
            <DataGridTextColumn Header="Phone Number" Width="auto" Binding="{Binding Path=PhoneNumber}" />
            <DataGridTextColumn Header="Date of Birth" Width="auto" Binding="{Binding Path=DateOfBirth}" />
            <DataGridTextColumn Header="Age" Width="auto" Binding="{Binding Path=Age}" IsReadOnly="True"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button x:Name="SaveToXML" Content="Save" Grid.Column="1" Grid.Row="3" Width="50" HorizontalAlignment="Left"/>
</Grid>

If you want to test the count number of the collection, you have to use a property which follows it:如果要测试集合的计数,则必须使用它后面的属性:

    private int initcount;
    public YourViewModel()//constructor
    {
        RecordNbr = People.Count;
        initcount = RecordNbr;
    }

    //Property whixh follows the number of count
    private int _recordNbr;
    public int RecordNbr
    {
        get => _recordNbr;
        set
        {
            _recordNbr = value;
            NotifyOfPropertyChange(() => RecordNbr);
            NotifyOfPropertyChange(() => CanSaveToXML);
        }
    }

    public void SaveToXML()
    {

    }

    public bool CanSaveToXML
    {
        get => initcount == RecordNbr;
    }

    public void RemoveItem()
    {
       if (People.Count == 0) return;

        People.RemoveAt(0);
        RecordNbr = People.Count;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM