我无法想象这是一个不常见的问题,但是经过几天的努力,却找不到答案。我的程序在启动时创建对象/属性。我有一个带有datagridcheckboxcolumn的绑定数据网格,该网格允许用户进一步更改对象属性。问题是在初始数据加载期间会触发propertychanged事件,而我只希望更改事件在用户使用复选框后才发生。任何帮助/方向将不胜感激。这是我的代码:
public class AvailableFolder
{
public string FolderPath { get; set; }
private bool folderIncluded { get; set; }
public bool FolderIncluded
{
get { return folderIncluded; }
set
{
folderIncluded = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string properName = null)
{
if (PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(properName));
}
System.Windows.MessageBox.Show("property changed");
}
}
在启动时,它循环并更新对象属性:
for (int i = 0; i < allFoldersList.Count; i++)
{
string xFolderName = allFoldersList[i].FolderName;
string xFolderPath = allFoldersList[i].FolderPath;
bool xFolderIncluded = allFoldersList[i].FolderIncluded;
if (xFolderIncluded == false)
{
for (int r = 0; r < MainWindow.openedProject.ReleaseFolders.Count; r++)
{
string releaseFolder = MainWindow.openedProject.ReleaseFolders[r].FullReleaseFolderName;
if (xFolderName == releaseFolder)
{
allFoldersList[i].FolderIncluded = true;
}
}
}
}
AllFoldersDataGrid.ItemsSource = allFoldersList;
还有我的datagrid xaml:
<DataGrid x:Name="AllFoldersDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Included" Binding="{Binding FolderIncluded,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="Folder" Binding="{Binding FolderName}"/>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="WhiteSmoke" />
<Style.Triggers >
<DataTrigger Binding="{Binding FolderIncluded}" Value="true">
<Setter Property="Background" Value="LightGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>