簡體   English   中英

WPF中關於資源字典的問題

[英]Issue About Resource Dictionary in WPF

這是我第一次使用資源字典,我創建了新的資源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit">
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border x:Name="BackgroundBorder" Background="Transparent">
                        <ContentPresenter VerticalAlignment="Center" Margin="4,0,6,0" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush" Value="{x:Null}"/>
    </Style>
</ResourceDictionary>

在我的窗戶(我想使用該樣式的地方)中,我聲明:

<ResourceDictionary x:Key="MyDictionary"
                    Source="/AlrakizaTouch;component/MyDictionary.xaml"/>

我的問題是如何使用帶有datagrid控件的樣式,我試過這個:

<DataGrid x:Name="dgItemsReceipt"
          CellStyle="{StaticResource ResourceKey=DataGridCell}"
          CanUserAddRows="True"
          ItemsSource="{Binding ocItemsinInvoice,Mode=TwoWay}"
          Margin="10,-1,0,176" Width="412" AutoGenerateColumns="False"
          RowHeight="34" CurrentCellChanged="dgItemsReceipt_CurrentCellChanged"
          SelectionMode="Single" ColumnHeaderHeight="50"
          Foreground="#FFCFCFCF" BorderBrush="#FFE8E8E8">
    <DataGrid.Columns>
    </DataGrid.Columns>
</DataGrid>

但是給我這個錯誤“資源DataGridCell無法解析”。

請幫忙。

在 Windows 資源字典中合並你的字典

<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="/AlrakizaTouch;component/MyDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

我非常懷疑尼特的回答。 我認為你的問題是:

CellStyle="{StaticResource ResourceKey=DataGridCell}"

代替

CellStyle="{StaticResource ResourceKey={x:Type DataGridCell}}"

此外,您不必只合並一本字典,您可以這樣做:

<Window.Resources>
   <ResourceDictionary Source="/AlrakizaTouch;component/MyDictionary.xaml"/>
</Window.Resources>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM