我正在尝试使用我的抽象类上的IndexerName属性重命名我的索引器属性.但它仍显示编译错误类型"XXX"已包含"项目"的定义.
抽象类:
public abstract class MyAbstract
{
[IndexerName("Indexer")]
public abstract string this[string propertyName]
{
get;
}
}
Run Code Online (Sandbox Code Playgroud)
具体类:
public class MyConcrete : MyAbstract
{
string item;
public string Item
{
get { return item; }
set { item = value; }
}
public override string this[string propertyName]
{
get { return propertyName; }
}
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
"MyConcrete"类型已经包含"Item"的定义.
我试图将IndexerName放在索引器覆盖上,它显示错误"无法在标记为覆盖的索引器上设置IndexerName属性"
如何在抽象类上使用IndexerName?
提前致谢.
我有一个资源字典,它是在其上定义的数据网格样式以及该数据网格样式内的样式。
<Style TargetType="{x:Type DataGrid}" x:Key="CatalogDataGrid">
<Style.Resources>
<Style TargetType="{x:Type DataGridCell}" x:Key="RightAlignedDataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border
Padding="5,0"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ContentPresenter
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="Center"
HorizontalAlignment="Right"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)
然后在我的 XAML 中我尝试使用RightAlignedDataGridCell以使我的列右对齐。
<DataGrid... Style="{StaticResource CatalogDataGrid}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}">
</DataGridTextColumn>
<DataGridTextColumn Header="Total" Binding="{Binding Total}"
CellStyle="{StaticResource RightAlignedDataGridCell}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,我收到资源未找到异常。如果我将该样式放在资源字典根上。它可以工作。但我想RightAlignedDataGridCell留在
<Style.Resources>里面CatalogDataGrid。
如何RightAlignedDataGridCell在我的 XAML 上使用它而不将其移动到资源字典根目录?
提前致谢。