我正在嘗試顯示具有不同物件型別的串列(例如狗和貓)。型別應該在視覺上有所不同(在 MainWindow.xaml 中的 DataTemplate 中通過紅色和藍色字體實作的示例中)。
此外,我希望既不能選擇貓,也不能在 IsMouseOver 觸發器上獲得彩色背景。但是,我在后者方面沒有成功。我在 MainWindow.xaml 中留下了一些我的嘗試作為注釋。
主視窗.xaml
<Window.Resources>
<local:AnimalToFocusConverter x:Key="AnimalToFocusConverter" />
<local:AnimalToBackgroundColorConverter x:Key="AnimalToBackgroundColorConverter" />
<DataTemplate DataType="{x:Type local:Dog}">
<TextBlock Text="{Binding Path=Name}" Foreground="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:Cat}">
<TextBlock Text="{Binding Path=Name}" Foreground="Blue" />
</DataTemplate>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding AnimalCollection}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Background" Value="Transparent" />
<!--<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid Background="Transparent">
<ContentPresenter />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding Converter={StaticResource AnimalToBackgroundColorConverter}}" />
</Trigger>
</Style.Triggers>-->
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
動物.cs
public class Animals
{
public ObservableCollection<IAnimal> AnimalCollection { get; set; } = new ObservableCollection<IAnimal>();
public Animals()
{
AnimalCollection.Add(new Dog());
AnimalCollection.Add(new Dog());
AnimalCollection.Add(new Cat());
AnimalCollection.Add(new Dog());
AnimalCollection.Add(new Cat());
}
}
IAnimal.cs
public interface IAnimal
{
}
狗.cs / Cat.cs
public class Dog : ObservableObject, IAnimal
{
private static int counter = 0;
public string Name { get; set; }
public Dog()
{
Name = $"Dog{ counter}";
}
}
轉換器.cs
[ValueConversion(typeof(IAnimal), typeof(bool))]
public class AnimalToFocusConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value switch
{
Dog => true,
Cat => false,
_ => false,
};
}
// ...
[ValueConversion(typeof(IAnimal), typeof(SolidColorBrush))]
public class AnimalToBackgroundColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value switch
{
Dog => new SolidColorBrush(Colors.LightBlue),
Cat => new SolidColorBrush(Colors.Transparent),
_ => new SolidColorBrush(Colors.LightBlue),
};
}
uj5u.com熱心網友回復:
您可以將物件的容器IsHitTestVisible屬性設定為:ListViewItemCatfalse
<ListView ItemsSource="{Binding AnimalCollection}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Background" Value="Transparent" />
<Setter Property="IsHitTestVisible"
Value="{Binding Converter={StaticResource AnimalToFocusConverter}}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/437173.html
上一篇:在WPFTextBlock中顯示ASCII藝術有奇怪的換行符
下一篇:用戶控制元件。找不到目標元素的管理FrameworkElement或FrameworkContentElement
