例如,在WebAPI应用程序中,有什么区别
[assembly: OwinStartup(typeof(MyClass), "MyMethod")]
Run Code Online (Sandbox Code Playgroud)
和
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyClass), "MyMethod")]
Run Code Online (Sandbox Code Playgroud)
?
当绑定到DataTemplate内的 SolidColorBrush属性时,我在Visual Studio输出窗口中收到此警告:
System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement.BindingExpression:路径= MyColor; 的DataItem = NULL; target元素是'SolidColorBrush'(HashCode = 22943289); 目标属性是'颜色'(类型'颜色')
如果我直接绑定在DataTemplate外部的矩形元素上,那么一切都很好.
任何人都可以从下面的示例代码中解释为什么这两个明显相似的用法存在差异:
我的看法:
<UserControl.Resources>
<vm:TestViewModel x:Key="_myTestVM"/>
<DataTemplate x:Key="testVMDataTemplate">
<Grid>
<Rectangle Height="30" Width="200" Margin="5">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=MyColor}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid>
<StackPanel DataContext="{StaticResource _myTestVM}">
<!-- Binding *outside* the DataTemplate = works fine -->
<Rectangle Height="30" Width="200" Margin="5">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=MyColor}"/>
</Rectangle.Fill>
</Rectangle>
<!-- Binding *inside* the DataTemplate = output warning -->
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource testVMDataTemplate}"/>
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我的ViewModel(TestViewModel):
public class TestViewModel …Run Code Online (Sandbox Code Playgroud)