开发者

WPF XAML Style markup shortcuts?

Is it possible to write this out in a slicker/shorter way? all it is doing is setting 3 properties and taking too much room IMO..

   <Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource BasicFont}">
        <Setter Property="Foreground">
            <Setter.Value>
                <Binding>
                    <Binding.Converter>
                        <local:ForegroundColorConverter />
                    </Binding.Converter>
                </Binding>
            </Setter.Value>
        </Setter>
        <Setter Property="Background">
            <Setter.Value>
                <Binding>
                    <Binding.Converter>
                        <local:ColorConverter />
                    </Binding.Converter>
                </Binding>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <Binding>
                    <Binding.Converter>
                        <local:ColorConverter />
                    </Binding.Converter>
             开发者_开发百科   </Binding>

            </Setter.Value>
        </Setter>
    </Style>


Sort of, first you have to define the Converters before the Style:

<local:ColorConverter x:Key="colorConverter" />

Then you can use them as StaticResources in your style:

<Setter Property="BorderBrush" Value="{Binding Converter={StaticResource colorConverter}}" />

Full example

C#:

using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    // The converter
    public class ColorConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Brushes.Red;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

}

XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="354">
    <Window.Resources>
        <local:ColorConverter x:Key="colorConverter" />
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="{Binding Converter={StaticResource colorConverter}}" />
        </Style>
    </Window.Resources>
    <Grid>
        <Button Margin="8" Content="A button" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</Window>

Btw, now that I think about it, not sure if you simplified your code, but you don't actually need a converter for that. You can set a SolidColorBrush instead of a Converter (unless you're doing some code in the converter), something like this:

<Window.Resources>
    <SolidColorBrush Color="Red" x:Key="redSolidColorBrush" />
    <SolidColorBrush Color="White" x:Key="whiteSolidColorBrush" />
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="{StaticResource redSolidColorBrush}" />
        <Setter Property="Foreground" Value="{StaticResource whiteSolidColorBrush}" />
    </Style>
</Window.Resources>
<Grid>
    <Button Margin="8" Content="A button" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>


Try this:

<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource BasicFont}">
    <Setter Property="Foreground" Value="{Binding Converter={local:ForegroundColorConverter}}"/>
    <Setter Property="Background" Value="{Binding Converter={local:ForegroundColorConverter}}"/>
    <Setter Property="BorderBrush" Value="{Binding Converter={local:ForegroundColorConverter}}"/>
</Style>

Not sure this is supported (don't have VS right here to test), but try it - that's at least a little less verbose :).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜