Wpf 使用类字段在xaml中设置值
我有一个Wpf 使用类字段在xaml中设置值,wpf,xaml,Wpf,Xaml,我有一个DataGrid作为: <DataGrid Grid.Row="4" Name="grvAllCry" Margin="5,5,5,5" ItemsSource="{Binding}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="Rank" Width="10*" > <DataGri
DataGrid
作为:
<DataGrid Grid.Row="4" Name="grvAllCry" Margin="5,5,5,5" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Rank" Width="10*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Rank}" Foreground="#46BF6E"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
有没有一种方法可以创建这样的类,并在许多不同的xaml文件中使用它的变量?例如:
<Label Content="{Binding Rank}" Foreground="MyGreen"></Label>
在xaml文件中,我不知道如何从.cs文件调用变量,请帮助我。您可以创建一个新的
ResourceDictionary
,在其中定义笔刷
资源:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="myBrush" Color="#46BF6E"/>
</ResourceDictionary>
…并使用StaticResource
标记扩展从任何视图引用资源:
<Label Content="{Binding Rank}" Foreground="{StaticResource myBrush}"></Label>
<Label Content="{Binding Rank}" Foreground="{StaticResource MyGreen}"/>
您可以创建一个新的
资源字典
,在其中定义画笔
资源:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="myBrush" Color="#46BF6E"/>
</ResourceDictionary>
…并使用StaticResource
标记扩展从任何视图引用资源:
<Label Content="{Binding Rank}" Foreground="{StaticResource myBrush}"></Label>
<Label Content="{Binding Rank}" Foreground="{StaticResource MyGreen}"/>
可以使用
{x:static…}
扩展来引用静态
属性或字段(包括常量
字段)。对于Config
类,它应该是:
<Label Content="{Binding Rank}" Foreground="{x:Static myNameSpace:Config.MyGreen}"/>
这些资源可以从StaticResource
/DynamicResource
扩展中使用:
<Label Content="{Binding Rank}" Foreground="{StaticResource myBrush}"></Label>
<Label Content="{Binding Rank}" Foreground="{StaticResource MyGreen}"/>
可以使用
{x:static…}
扩展来引用静态
属性或字段(包括常量
字段)。对于Config
类,它应该是:
<Label Content="{Binding Rank}" Foreground="{x:Static myNameSpace:Config.MyGreen}"/>
这些资源可以从StaticResource
/DynamicResource
扩展中使用:
<Label Content="{Binding Rank}" Foreground="{StaticResource myBrush}"></Label>
<Label Content="{Binding Rank}" Foreground="{StaticResource MyGreen}"/>
ResourceDictionary是您的答案。然后将变量用作StaticResource MyGreen
。这里的答案可能是ResourceDictionary的副本。然后将变量用作StaticResource MyGreen