我創建了一個名為 TreeViewTestItem 的自定義 TreeViewItem。在此 TreeViewTestItem 中有自定義屬性,其中一個用于在 TreeViewTestItem 的標題中顯示影像:
class TreeViewTestItem : TreeViewItem, INotifyPropertyChanged
{
public ImageSource _image;
public ImageSource Image
{
get
{
return _image;
}
set
{
_image = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Image"));
}
}
static TreeViewTestItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TreeViewTestItem), new FrameworkPropertyMetadata(typeof(TreeViewTestItem)));
}
public TreeViewTestItem()
{
PreviewMouseDown = TreeViewTestItem_PreviewMouseDown;
_image = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/DEBUG;component/Resources/Icons/square.png"));
}
}
這是我在 Themes 檔案夾中的 Generic.xaml 檔案:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-DEBUG.Views.Controls">
<Style TargetType="{x:Type local:TreeViewTestItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,3">
<Image Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:TreeViewTestItem}}, Path=Image, Mode=TwoWay}" VerticalAlignment="Center" Width="30" Height="30" />
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Path=Header}" VerticalAlignment="Center" Margin="5,0,0,0" FontSize="18" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
這是 TreeView 應該顯示的視圖:
<Window x:Class="DEBUG.Views.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:src="clr-namespace:DEBUG.Views.Controls"
xmlns:local="clr-namespace:DEBUG.Views"
mc:Ignorable="d">
[...]
<TreeView ItemsSource="{Binding TreeViewTests}">
</TreeView>
</Window>
這里是我如何填寫我的 TreeViewTests 屬性:
class TestViewModel : MVVMHelper
{
public ObservableCollection<TreeViewTestItem> TreeViewTests { get; set; }
[...]
public TestViewModel(List<Test> testList)
{
TreeViewTests = new ObservableCollection<TreeViewTestItem>();
}
private void AddTestNode(TreeViewTestItem treeItem, List<Test> testList)
{
foreach (Test test in testList)
{
TreeViewTestItem tvi = new TreeViewTestItem();
tvi.Test = test;
tvi.Header = test.LibelleEn;
if (treeItem == null)
{
TreeViewTests.Add(tvi);
}
else
{
treeItem.Items.Add(tvi);
}
AddTestNode(tvi, test.ListeTest);
}
}
}
Test 屬性是另一個目前無關緊要的物件。
現在我的問題:如果我DefaultStyleKeyProperty.OverrideMetadata從我的 TreeViewTestItem 中的靜態建構式中洗掉,這些專案將顯示在我的 TestView 的 TreeView 中。如果我使用它,我的 TreeView 是空的。
我試圖將我的自定義專案移動到 Themes 檔案夾中,但沒有成功。我試圖直接在我的 TestView 中設定樣式,但它也沒有奏效。我的影像的路徑是正確的。
根本沒有拋出例外,退出視窗也看不到例外。
有沒有人知道如何讓我的自定義 TreeViewItem 顯示在我的 TreeView 中?
uj5u.com熱心網友回復:
問題是專案未加載/查找 Generic.xaml 檔案。
原因是這個專案是一個舊的 Winform 專案轉換為 XAML 專案,AssemblyInfo.cs 中缺少一行:
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
添加此行后,一切正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524082.html
上一篇:訪問嵌套的XAML元素
