对不起,我再次错误地忽略了一些细节。以下是按照正确的方式定义转换器及使用方法的完整代码示例:
Model:
public class Item
{
public string ItemType { get; set; }
public string Value { get; set; }
}
ViewModel:
public class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<Item> Items { get; set; }
public ObservableCollection<ItemType> ItemTypes { get; set; }
private bool _isEditMode;
public bool IsEditMode
{
get { return _isEditMode; }
set
{
if (_isEditMode != value)
{
_isEditMode = value;
OnPropertyChanged(nameof(IsEditMode));
}
}
}
public MainViewModel()
{
ItemTypes = new ObservableCollection<ItemType>
{
new ItemType {Name = "Type 1"},
new ItemType {Name = "Type 2"},
new ItemType {Name = "Type 3"}
};
Items = new ObservableCollection<Item>
{
new Item { ItemType = "Type 1", Value = "Value 1" },
new Item { ItemType = "Type 2", Value = "Value 2" },
new Item { ItemType = "Type 3", Value = "Value 3" }
};
IsEditMode = false;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Converter:
public class InverseBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool b)
{
return !b;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool b)
{
return !b;
}
return false;
}
}
public class InverseBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool b)
{
return b ? Visibility.Collapsed : Visibility.Visible;
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
View:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels="clr-namespace:WpfApp1.ViewModels"
xmlns:converters="clr-namespace:WpfApp1.Converters"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
<converters:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" IsReadOnly="{Binding IsEditMode, Converter={StaticResource InverseBooleanConverter}}"
SelectionUnit="CellOrRowHeader" SelectedCellsChanged="DataGrid_SelectedCellsChanged">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Type" SelectedValueBinding="{Binding ItemType}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.ItemTypes}" />
<Setter Property="DisplayMemberPath" Value="Name" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="IsHitTestVisible" Value="False" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.ItemTypes}" />
<Setter Property="DisplayMemberPath" Value="Name" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTemplateColumn Header="Value">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Value, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Edit" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="10"
Click="EditButton_Click"
Visibility="{Binding IsEditMode, Converter={StaticResource InverseBooleanToVisibilityConverter}, Mode=OneWay}"/>
<Button Content="Save" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="10"
Click="SaveButton_Click"
Visibility="{Binding IsEditMode, Converter={StaticResource InverseBooleanToVisibilityConverter}, Mode=OneWay}"/>
</Grid>
</Window>
在此示例中,我们在View定义了两个静态资源:InverseBooleanConverter和InverseBooleanToVisibilityConverter。
别忘了在命名空间后加上你的转换器命名空间。你可以在Window上加上以下命名空间:
xmlns:converters="clr-namespace:WpfApp1.Converters"
如果你希望在其他的View中使用上述转换器,你需要在其根元素中再次声明这些资源。在这个例子中,我们再次将这些资源添加到了Window中。
在DataGrid或Button等WPF控件的属性中使用转换器时,需要使用以下格式的语法:
{Binding IsEditMode, Converter={StaticResource InverseBooleanToVisibilityConverter}, Mode=OneWay}
在上面的示例代码中,IsEditMode属性绑定到此属性的反向布尔值转换为Visibility值。使用StaticResource来引用之前我们在资源中定义的转换器。
我希望这次回答足够完整,感谢您的耐心等待