只是想知道他們的任何示例或元素可以看起來非常類似於Visual Studio中使用的屬性面板?我猜想Visual Studio 2010中的一個是建立在WPF之上的,幾乎肯定是一個treeview?WPF屬性面板類似於Visual Studio的
7
A
回答
4
- Mindscape有一個免費的。
- 有一個CodeProject。
- Actipro發展很好。
- 與ComponentOne一樣。
3
如果你想使用一些簡單的XAML,以下是視覺上相同的的WinForms PropertyGrid的,但更容易使用的工作:
<Style x:Key="InnerBorder" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Margin" Value="4" />
<Setter Property="BorderBrush" Value="#B4B0A8" />
</Style>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Main property grid area -->
<Border Style="{StaticResource InnerBorder}">
<ListBox
ItemsSource="{Binding Parameters}"
IsSynchronizedWithCurrentItem="True"
KeyboardNavigation.TabNavigation="Continue"
HorizontalContentAlignment="Stretch" BorderThickness="0">
<!-- Category grouping rows -->
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Background="#D4D0C8" FontWeight="Bold" Padding="2 2 0 4" Margin="0 0 0 3"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style>
<Setter Property="Control.Margin" Value="0 0 0 8" />
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
<!-- Item container style -->
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Focusable" Value="False" />
<Setter Property="TabIndex" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<DockPanel Margin="4 0 0 0" IsKeyboardFocusWithinChanged="DockPanel_IsKeyboardFocusWithinChanged" MouseDown="DockPanel_MouseDown">
<TextBlock Name="TitleBlock" Text="{Binding DisplayName}" Width="135" />
<ContentPresenter />
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="TitleBlock" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter TargetName="TitleBlock" Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Border>
<!-- Help area -->
<Border Style="{StaticResource InnerBorder}" Grid.Row="1" DataContext="{Binding Parameters}">
<StackPanel HorizontalAlignment="Stretch" Margin="2">
<TextBlock FontWeight="Bold" Text="{Binding /DisplayName}" />
<TextBlock Text="{Binding /Description}" TextWrapping="Wrap" />
</StackPanel>
</Border>
</Grid>
這裏是背後
private void DockPanel_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var element = (FrameworkElement)sender;
if(element.IsKeyboardFocusWithin)
{
Visual cur = element;
while(cur!=null && !(cur is ListBoxItem))
cur = (Visual)VisualTreeHelper.GetParent(cur);
((ListBoxItem)cur).IsSelected = true;
}
}
private void DockPanel_MouseDown(object sender, MouseEventArgs e)
{
((FrameworkElement)sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
}
private void InitializeView()
{
var view = CollectionViewSource.GetDefaultView(Parameters);
if(view.GroupDescriptions.Count==0)
view.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
if(view.SortDescriptions.Count==0)
{
view.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending));
}
}
代碼
此屬性網格更適合在WPF中使用的原因是,只要具有類別,顯示名稱和說明,您就可以將任何類型的對象添加到參數集合中。
如果您想使用它來實際顯示特定對象的屬性,只需要幾行就可以使用適當的對象加載Parameters集合。
+0
我已經嘗試編譯這個代碼,在一個空的項目中,並將數據集綁定到Parameters對象。不過,我得到'System.Windows.StaticResourceExtension提供的值'的錯誤引發了一個異常。'在啓動。打破網格定義;
+0
的''
+1 - 我也需要一個;) – TomTom 2010-05-12 16:13:09