关于wpf设置属性的几种方式

本文介绍了WPF中不同级别的样式应用方法,包括控件特定样式、局部子控件样式、窗口范围样式、应用程序范围样式及明确使用的样式。通过具体代码示例展示了如何使样式在不同层级上生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意:这个设置没有优先级,同一个属性只能设置一次。


1 Local control specific style


这种方式仅仅只是对这个控件有效!
==================================code
<Grid Margin="10">
        <TextBlock Text="Style test">
            <TextBlock.Style>
                <Style>
                    <Setter Property="TextBlock.FontSize" Value="36" />
                </Style>
            </TextBlock.Style>
        </TextBlock>
</Grid>
======================================


2 Local child control style


 这种做法会对Resources的子控件或者子控件的子控件起作用。这对于更多的本地需求是很好的,你只需要一组控件看起来相同,而不是在每个
 对象上设计单独属性
===================================code
 <StackPanel Margin="10">
        <StackPanel.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="Gray" />
                <Setter Property="FontSize" Value="24" />
            </Style>
        </StackPanel.Resources>
        <TextBlock>Header 1</TextBlock>
        <TextBlock>Header 2</TextBlock>
        <TextBlock Foreground="Blue">Header 3</TextBlock>
   </StackPanel>
   
3 Window-wide styles
定义windows中的资源样式,在上级别的范围上有增强了范围


===========================================
<Window x:Class="WpfTutorialSamples.Styles.WindowWideStyleSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowWideStyleSample" Height="200" Width="300">
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Gray" />
            <Setter Property="FontSize" Value="24" />
        </Style>
    </Window.Resources>
    <StackPanel Margin="10">
        <TextBlock>Header 1</TextBlock>
        <TextBlock>Header 2</TextBlock>
        <TextBlock Foreground="Blue">Header 3</TextBlock>
    </StackPanel>
</Window>


4 Application-wide styles 
如果您希望您的样式在整个应用程序中在不同的窗口中使用,您可以为整个应用程序定义它。
 这是在Visual Studio可能为您创建的App.xaml文件中完成的
 ==============================code
 <Application x:Class="WpfTutorialSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="Styles/WindowWideStyleSample.xaml">
    <Application.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Gray" />
            <Setter Property="FontSize" Value="24" />
        </Style>
    </Application.Resources>
</Application>
======================
<Window x:Class="WpfTutorialSamples.Styles.WindowWideStyleSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ApplicationWideStyleSample" Height="200" Width="300">
    <StackPanel Margin="10">
        <TextBlock>Header 1</TextBlock>
        <TextBlock>Header 2</TextBlock>
        <TextBlock Foreground="Blue">Header 3</TextBlock>
    </StackPanel>
</Window>


5 Explicitly using styles 


这种方式只有同时满足key和targetType的时候,样式才生效。
===============================================================
<Window x:Class="WpfTutorialSamples.Styles.ExplicitStyleSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ExplicitStyleSample" Height="150" Width="300">
    <Window.Resources>
        <Style x:Key="HeaderStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="Gray" />
            <Setter Property="FontSize" Value="24" />
        </Style>
    </Window.Resources>
    <StackPanel Margin="10">
        <TextBlock>Header 1</TextBlock>
        <TextBlock Style="{StaticResource HeaderStyle}">Header 2</TextBlock>
        <TextBlock>Header 3</TextBlock>
    </StackPanel>
</Window>

在C# WPF中,绑定命令有几种常见的方式,主要包括以下几种: 1. **使用ICommand接口**: - 创建一个实现了ICommand接口的类,通常包括Execute方法和CanExecute方法。 - 在ViewModel中实例化这个命令类,并将其暴露为公共属性。 - 在XAML中通过`Command`属性绑定到这个命令。 ```csharp public class RelayCommand : ICommand { private readonly Action<object> _execute; private readonly Predicate<object> _canExecute; public RelayCommand(Action<object> execute, Predicate<object> canExecute = null) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; } public bool CanExecute(object parameter) => _canExecute?.Invoke(parameter) ?? true; public void Execute(object parameter) => _execute(parameter); public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } } ``` ```xml <Button Content="Click Me" Command="{Binding MyCommand}" /> ``` 2. **使用RoutedCommand**: - 使用WPF内置的`RoutedCommand`,通过`CommandBindings`和`InputBindings`在XAML中绑定命令。 - 在代码后置(Code-Behind)中实现命令处理逻辑。 ```xml <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.New" Executed="NewCommand_Executed" CanExecute="NewCommand_CanExecute" /> </Window.CommandBindings> <Button Content="New" Command="ApplicationCommands.New" /> ``` ```csharp private void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e) { // Command logic } private void NewCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } ``` 3. **使用MVVM框架**: - 使用MVVM框架(如MVVM Light、Prism等)提供的命令绑定功能,这些框架通常提供了更高级的命令绑定机制和工具。 - 例如,MVVM Light提供了`RelayCommand`,Prism提供了`DelegateCommand`。 ```csharp public class MyViewModel { public ICommand MyCommand { get; } public MyViewModel() { MyCommand = new RelayCommand(OnMyCommandExecuted, CanMyCommandExecute); } private void OnMyCommandExecuted(object parameter) { // Command logic } private bool CanMyCommandExecute(object parameter) { return true; } } ``` ```xml <Button Content="Click Me" Command="{Binding MyCommand}" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值