所以你可以看到 ID 列应该被隐藏但它不是。并且调试器将 xaml 文件中 IDVisible 的值显示为“隐藏”,但它从未进入窗口。我也在调用属性更改方法。我做错了什么,ID 列应该消失,或者至少这就是我想要做的。
这是 MainWindowViewModel.cs 的代码
namespace MagicDB
{
class MainWindowViewModel : ObservableObject
{
private CardDB _cards;
private Command _InitCardDB;
private Visibility _IDVisible;
public CardDB Cards
{
get { return _cards; }
set { _cards = value; OnPropertyChanged("Cards"); }
}
public Visibility IDVisible
{
get { return _IDVisible; }
set { _IDVisible = value; VerifyPropertyName("IDVisible"); OnPropertyChanged("IDVisible"); }
}
public MainWindowViewModel()
{
IDVisible = Visibility.Hidden;
_InitCardDB = new Command(InitDB, true);
Cards = new CardDB();
}
和 xaml 文件....
<Window x:Class="WpfDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="500" Height="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="450*" />
</Grid.RowDefinitions>
<DataGrid AutoGenerateColumns="False"
HorizontalAlignment="Left"
Name="dataGrid1"
VerticalAlignment="Top"
ItemsSource="{Binding Cards.cardDB}"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
CanUserResizeRows="False"
CanUserSortColumns="True"
AlternatingRowBackground="LightBlue"
Width="480" Height="auto" Grid.Row="1" IsSynchronizedWithCurrentItem="True"
>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding CardID}" Width="25" Visibility="{Binding IDVisible,Mode=TwoWay}"></DataGridTextColumn>