【问题标题】:Caliburn.Micro MVVM WPF - enable a button when DataGrid has been edited by the user, otherwise disable itCaliburn.Micro MVVM WPF - 在用户编辑 DataGrid 时启用按钮,否则禁用它
【发布时间】:2020-09-12 14:35:19
【问题描述】:

我是 MVVM 和 Caliburn.Micro 的新手。

我已经实现了一个绑定到 <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; }
}

此数据在启动时从 XML 文件加载,也可以通过单击连接到方法 SaveToXML() 和显然连接到 CanSaveToXML() 的按钮“保存”将其保存到 XML 文件中。保存和加载工作完美,但我想让“保存”按钮仅在用户编辑 DataGrid 时启用,否则它应该保持禁用状态。

我尝试将 CustomerModel 类的当前对象数与启动时 CustomerModel 的对象数进行比较。如果数字不同,它应该启用按钮,因为它表示有人输入或删除了一个条目。

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

在哪里

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

问题是Customers.Count() 只执行一次,并且在启动和用户添加/删除条目后数字不会更新。我知道它可能与

有关
NotifyOfPropertyChange(() => ???)

但我不知道如何将它与 Customers.Count() 的对象数量相关联

欢迎提出任何想法。

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>

【问题讨论】:

    标签: c# wpf mvvm caliburn.micro


    【解决方案1】:

    如果你想测试集合的计数,你必须使用它后面的属性:

        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;
        }
    

    【讨论】:

    • 感谢您的回答,尽管它仍然无法正常工作。我不明白每次有人更改客户数量时如何更新 RecordNbr。我应该将它作为参数之一添加到 CustomerModel 类吗?
    • CustomerModel 是数据。在我的回答中,RecordNbr 是链接到视图的视图模型的属性,其中定义了您的方法 SaveToXml。不知道您的视图模型的名称,在您删除或添加的位置显示您的代码
    • 添加或删除记录时,必须将 RecordNbr 设置为 count 的新值。您在示例中看到过我的方法 RemoveItem
    • 我知道,我已经看到了,这很有意义,尽管我没有删除或添加的方法,因为它只是由 DataGrid 完成的。我的 ViewModel 称为 MainViewModel。
    • 所以每次你有一个计数变化,你必须设置属性,按钮是否启用。如果你直接在数据网格上工作,你必须找到一个你可以捕获的事件设置 RecordNbr ...它的功能?那么如果你想测试xml文件的变化,你可以使用Hashcode,但前提是你的xml不是太大..所以如果你不知道怎么做就问新问题..
    猜你喜欢
    • 2022-08-03
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2013-11-23
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多