0
我有一個自定義樹視圖設置,我從另一個項目,我想用作我的樹的節點/節點的UserControl。這是我目前有:如何將自定義用戶控件放入WPF中的HierarchicalDataTemplate中?
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GraphStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<XmlDataProvider x:Key="nodes" Source=".\nodes.xml" XPath="Node"/>
<HierarchicalDataTemplate DataType="Node" ItemsSource="{Binding XPath=Children/Node}">
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Width="200" Height="60">
<Grid Background="Red" Height="35" VerticalAlignment="Top" Margin="5,0,5,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="38*"/>
<ColumnDefinition Width="125*"/>
<ColumnDefinition Width="37*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="textBox" Grid.Column="1" TextWrapping="Wrap" Text="TextBox" TextAlignment="Left" Height="35"/>
<Ellipse x:Name="buttonAddQuestionNode" Grid.Column="1" Height="12" Margin="57,45,56,-22" Stroke="Black" VerticalAlignment="Top" Width="12">
<Ellipse.Fill>
<ImageBrush ImageSource="/BlogSample;component/Icons/addIcon.png"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="A" TextAlignment="Center" FontSize="25" Height="35"/>
<Path Grid.Column="1" Data="M100,35 L100,45" Fill="#FFF4F4F5" Height="11" Margin="62,0,62,-11" Stretch="Fill" Stroke="Black" VerticalAlignment="Bottom"/>
</Grid>
</UserControl>
</HierarchicalDataTemplate>
</ResourceDictionary>
</Window.Resources>
<TreeView ItemContainerStyle="{StaticResource GraphStyle}"
ItemsSource="{Binding Source={StaticResource nodes}}"/>
</Window>
HierarchicalDataTemplate下的部分是我把我的usercontrol作爲節點。我的問題是有一種方法來簡單地引用usercontrol而不是像這樣的代碼?還是有更好的方法來做到這一點我不知道?
我用正確的命名空間的設置它,這樣並沒有什麼顯示爲錯誤的,當我建立,但是當我運行它,我只得到連接的節點,而不是節點他們的自我線。這就像樹視圖根本看不到樣式,所以只是留下空白。我究竟做錯了什麼? – user3258394
是的。我雙重檢查我的命名空間,以確保我不會在某處丟失參考。 – user3258394
這條線應該是什麼:<! - 你的MainControlow中的UserControl代碼 - >? – user3258394