Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过完全限定的对象名而不是字符串访问资源_C#_Wpf - Fatal编程技术网

C# 通过完全限定的对象名而不是字符串访问资源

C# 通过完全限定的对象名而不是字符串访问资源,c#,wpf,C#,Wpf,我正在运行时更改控件的样式。如果用户处于“查看模式”,则将文本框更改为只读(从而更改文本框上影响显示的其他属性) 假设我在应用程序资源中有一种风格 <Style x:Key="ReadOnlyTextBoxStyle" TargetType="{x:Type TextBox}"> <Setter Property="BorderBrush" Value="Black"/> <Setter Property="BorderThi

我正在运行时更改控件的样式。如果用户处于“查看模式”,则将文本框更改为只读(从而更改文本框上影响显示的其他属性)

假设我在应用程序资源中有一种风格

    <Style x:Key="ReadOnlyTextBoxStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="Cursor" Value="Arrow"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="IsReadOnly" Value="True"/>
        <Setter Property="TextBlock.FontWeight" Value="Bold"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Visible"/>
        <Setter Property="TextWrapping" Value="Wrap"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
                        <ScrollViewer Name="PART_ContentHost"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
但我找不到通过以下方式访问资源的示例:

myControl.Style = (Style).Resources[nameOf(IsThere.Some.Way.To.Get.QualifiedReferenceToStyleResourceKey)];
如果我使用类似于:

myControl.Style = (Style).Resources["ReadOnlyTextBoxStyle"];
然后我将密钥更新为一个新字符串,比如“ReadOnlyTextBoxStyleModified”,然后我必须在代码中引用它的任何地方对其进行更改。如果我错过了其中一个替换,在运行时之前不会出现错误。如果可能的话,我希望在编译时看到错误

下面链接问题中的答案建议使用命名常量,但没有提供示例。这似乎可以实现我的目标,但我不明白如何实现他的评论

此外,一个好的做法是创建一个字符串常量,该常量映射资源字典中键的名称(以便您只能在一个位置更改它)


您可以为资源键创建一个包含静态字符串成员的类,如

public static class ResourceKeys
{
    public static string Key1 = "Key1";
}
然后使用这样的密钥声明一个资源,例如

<Window.Resources>
    <SolidColorBrush x:Key="{x:Static local:ResourceKeys.Key1}" Color="Red"/>
</Window.Resources>

我猜资源只能使用x:key值。此外,在运行时更改样式可能不需要代码隐藏(在某些情况下)。您可以使用触发器。@Pratekshrivastava感谢您的回复。即使使用一个在运行时之前不会通知我错误的字符串,也要比使用触发器更干净。@Clemens,我确实看到了答案的第二条注释。评论员要求回答海报详细说明,但回答海报没有回应。我想知道有没有人知道答案海报上说的是什么。如果他说这是一个好的做法,我想这是他以前做过的事情。@Clemens,谢谢你的回答。那么,您是说使用限定引用无法访问代码中的资源吗?我必须使用一个资源键字符串在代码中引用它?是的,像“grid.Background=(Brush)FindResource(ResourceKeys.Key1);”这样的东西正是我想要的
<Window.Resources>
    <SolidColorBrush x:Key="{x:Static local:ResourceKeys.Key1}" Color="Red"/>
</Window.Resources>
<Grid Background="{StaticResource ResourceKey={x:Static local:ResourceKeys.Key1}}">
grid.Background = (Brush)FindResource(ResourceKeys.Key1);