這是我在這里的第一個問題,所以請耐心等待。
我很困惑為什么這個 ToggleButton 列在切換時不傳達對 Match 模型 Reviewed 屬性的更改:
<ToggleButton
Height="auto" Foreground="White" Margin="0, 0, 10, 0"
IsChecked="{Binding Reviewed, Mode=TwoWay}">
</ToggleButton>
但是,此列可以很好地與 Match 模型 Reviewed 屬性進行通信
<DataGridCheckBoxColumn Header="Test" Width="auto" Binding="{Binding Reviewed, Mode=TwoWay}"/>
我將發布一些基本代碼以幫助理解我的問題。
XAML 資料網格示例
- ToggleButton 不傳達更改
- DataGridCheckBoxColumn 效果很好
<DataGrid ItemsSource="{Binding Path=ListOfBestMatches}" x:Name="DataGrid_Matches" Height="auto" Width="Auto" Margin="0,0,-4,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" AutoGenerateColumns="False">
<!-- Column Headers -->
<DataGrid.Columns>
<DataGridTemplateColumn Header="Reviewed">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ToggleButton
Height="auto" Foreground="White" Margin="0, 0, 10, 0"
IsChecked="{Binding Reviewed, Mode=TwoWay}">
</ToggleButton>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn Header="Test" Width="auto" Binding="{Binding Reviewed, Mode=TwoWay}"/>
</DataGrid.Columns>
</DataGrid>
視圖模型
public class MainWindowViewModel : BaseViewModel
{
/// <summary>
/// Property/field which holds the official list of best matches and bound to a datagrid
/// </summary>
private ObservableCollection<Match> _listOfBestMatches;
public ObservableCollection<Match> ListOfBestMatches
{
get => _listOfBestMatches;
set
{
if (_listOfBestMatches != value)
{
_listOfBestMatches = value;
this.OnPropertyChanged("ListOfBestMatches");
}
}
}
}
模型
public class Match : BaseViewModel
{
/// <summary>
/// Property which represents the state of the Check Toggle Box in the Best Matches Data grid Reviewed column
/// </summary>
private bool reviewed;
public bool Reviewed
{
get { return reviewed; }
set
{
reviewed = value;
OnPropertyChanged("Reviewed");
Debug.WriteLine("=== SET REVIEWED ===");
}
}
}
基本視圖模型
namespace UserFolders.ViewModels
{
/// <summary>
/// This is the base view model which all other view models will extend from.
/// This model is necesarry for notifying the UI when data has changed so it
/// can display appropriately on its own
/// </summary>
public class BaseViewModel : INotifyPropertyChanged
{
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
uj5u.com熱心網友回復:
終于找到了解決您問題的方法。我想,問題在于資料網格單元的更新方式。例如,我添加了一些文本列,并想知道它們只有在資料網格單元格失去焦點時才會更新。UpdateSourceTrigger=PropertyChanged(我以前用過,但這些東西很容易忘記^^)成功了。
這是更改后的代碼以及用于測驗的文本列:
<DataGrid ItemsSource="{Binding Path=ListOfBestMatches}" x:Name="DataGrid_Matches" Height="auto" Width="Auto" Margin="0,0,-4,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" AutoGenerateColumns="False"
>
<!-- Column Headers -->
<DataGrid.Columns>
<DataGridTextColumn Header="name" Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="name" Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTemplateColumn Header="Reviewed">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ToggleButton
Height="auto" Foreground="White" Margin="0, 0, 10, 0"
IsChecked="{Binding Reviewed, UpdateSourceTrigger=PropertyChanged}">
</ToggleButton>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn Header="Test" Binding="{Binding Reviewed, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
</DataGrid>
您只需將相應的屬性添加到您的虛擬機(或洗掉它):
private string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged("Name");
}
}
順便說一句:如果您有興趣,我前段時間為一門課程制作了一個 MVVM 模式示例專案。例如,它顯示了ViewModelBase該類的一些更新示例以及其他一些內容。你可以在這里找到它:https ://github.com/TheRealRolandDeschain/DesignPatternsMVVMExample
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/472166.html
