如何为样式指定设计器数据上下文,以便 Resharper 找到我的属性? [英] How can i specify a designer datacontext for a style, so Resharper finds my properties?
问题描述
我经常将 TreeViewItem 的 IsExpanded 和 IsSelected 属性绑定到我的视图模型.例如,这使得可以在加载树时预先展开项目或在选择项目时展开项目.
I often bind the IsExpanded and IsSelected properties of a TreeViewItem to my viewmodel. This for example makes it possible to make an item pre-expanded when the tree is loaded or expand an item when it gets selected.
XAML 如下所示:
<Window x:Class="StyleSetterDatatypeTest.MainWindow"
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"
xmlns:test="clr-namespace:StyleSetterDatatypeTest"
Title="MainWindow" Height="350" Width="525"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance test:TestViewModel, IsDesignTimeCreatable=True}">
<TreeView ItemsSource="{Binding Items}">
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding ItemExpanded}"/>
<Setter Property="IsSelected" Value="{Binding ItemSelected}"/>
</Style>
<HierarchicalDataTemplate DataType="{x:Type test:TestItemViewModel}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Window>
我的视图模型可能如下所示:
And my viewmodel could look like this:
public class TestItemViewModel
{
public bool ItemExpanded { get; set; }
public bool ItemSelected { get; set; }
public string Name { get; set; }
public string[] Children
{
get { return new [] {"Child 1", "Child 2"}; }
}
}
这在执行和设计器中运行良好,但 Resharper 没有在 Bindings 中找到 ItemSelected 和 ItemExpanded 属性,并将它们加下划线作为警告.
我可以理解为什么它找不到它们(我从未将TestViewModel"指定为 Style 的 Datacontext 类型),但是我该如何解决这个问题?没有样式-设计-数据上下文这样的东西...
This works fine in execution and designer, but Resharper does not find the ItemSelected and ItemExpanded properties in the Bindings and underlines them as a warning.
I can understand why it doesn't find them (i never specified "TestViewModel" as the Datacontext type for the Style), but how can i fix this? There is no such thing as a Style-Design-Datacontext...
更新:
这里的问题是,样式是在 TreeView 中定义的,并且 DataContext 明确设置为 TestViewModel.检查器没有得到,我的样式是 TreeViewItem 并且该项目具有 TestItemViewModel 的 DataContext(ItemsSource 元素的类型).
The problem here is, the style is defined in the TreeView and there the DataContext is clearly set to a TestViewModel. The checker doesn't get, that what I style is a TreeViewItem and this item has a DataContext of TestItemViewModel (Type of an ItemsSource element).
哦,我还尝试在 TreeView.ItemContainerStyle 中设置样式而不是 TreeView.Resources(这里应该清楚 DataContext 必须是 TextItemViewModel),但这不会改变任何东西...
Oh, and I also tried setting the style in TreeView.ItemContainerStyle instead if TreeView.Resources (here it should be clear the DataContext has to be a TextItemViewModel), but that doesn't change anything...
推荐答案
@lhildebrandt 的回答通常是正确的,但在我的情况下,此解决方案产生的错误完全禁止在设计器中显示视图.指定
inside 标签对我有帮助.
@lhildebrandt's answer is generally right, but in my case this solution produces errors that totally disable displaying the view in designer. Specifying <d:Style.DataContext>
inside <Style>
tag helped me.
<Style>
<d:Style.DataContext>
<x:Type Type="local:MyTreeItem" />
</d:Style.DataContext>
<!--usual setters, triggers, etc.-->
</Style>
这样也可以为控件指定d:DataContext
,我们可以为它提供接口、嵌套类甚至泛型,而不会出现任何错误:https://stackoverflow.com/a/46637478/5598194
In this way d:DataContext
can also be specified for controls, and we can provide it interfaces, nested classes and even generics without any errors:
https://stackoverflow.com/a/46637478/5598194
这篇关于如何为样式指定设计器数据上下文,以便 Resharper 找到我的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!