使用以下示例R#(resharper)無法找到Row樣式的datacontext並警告錯誤的綁定(在運行時工作正常)。好像風格是沒有得到的ItemsSource的DataContext的:在DataGrid.RowStyle上設置DataContext
MainWindow.xaml:
<Window x:Class="TestDatacontext.MainWindow"
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:testDatacontext="clr-namespace:TestDatacontext"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance testDatacontext:MainWindowVM}" >
<DataGrid ItemsSource="{Binding Items}" >
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" >
<Setter Property="Header" Value="{Binding Name}" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
</Window>
MainWindowVM:
using System.Collections.ObjectModel;
namespace TestDatacontext
{
class MainWindowVM
{
public ObservableCollection<ItemVM> Items { get; private set; }
}
}
ItemVM:
namespace TestDatacontext
{
class ItemVM
{
public string Name { get; set; }
}
}
感謝澄清,但...我無法看到ItemsSource == 1行的關係1項無效,如果是您可以將RowStyle的DataContext設置爲每行之一不能? –
ReSharper不知道「行」的語義是什麼,樣式將被控件使用(或者它會被使用嗎?)。就像這裏一樣,R#不知道'ColumnHeaderStyle'是什麼,它與包含控件的'ItemsSource'有什麼關係:https://gist.github.com/4125475 – ControlFlow
我明白了......謝謝! –