是否可以将参数(绑定)传递给 WPF 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12464589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:23:29  来源:igfitidea点击:

Is it possible to pass a parameter (a Binding) to a WPF Style

c#wpfxamlstylescontroltemplate

提问by Joe W

I have a WPF style that I am applying to list box items. Currently, each item is bound to a KeyValuePair. I display the Key in a TextBlock inside the ListBoxItem:

我有一个 WPF 样式,可用于列表框项目。目前,每个项目都绑定到一个 KeyValuePair。我在 ListBoxItem 内的 TextBlock 中显示 Key:

<TextBlock Name="Label" Text="{Binding Key}" />

What I want to do is make the Style generic so that if the data is not a KeyValuePair, (maybe just a string), I can bind the data correctly to the TextBlock.

我想要做的是使 Style 通用,以便如果数据不是 KeyValuePair(可能只是一个字符串),我可以将数据正确绑定到 TextBlock。

Is there a way to pass a parameter to a Style or DataTemplate or make the data binding generic?

有没有办法将参数传递给 Style 或 DataTemplate 或使数据绑定通用?

My style:

我的风格:

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ListBoxItem">
        <Border x:Name="Border" Padding="2" SnapsToDevicePixels="true" CornerRadius="4" BorderThickness="1">
          <TextBlock Name="Label" Text="{Binding Key}" />
        </Border>
        <ControlTemplate.Triggers>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsSelected" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectionGradient}" />
          </MultiTrigger>
        <ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

采纳答案by Damascus

Let me give you a quick example. Let's say your TextBoxcontains a number, and you want it to be with a red Background if the number is negative, or with a green background if >= 0

让我举一个简单的例子。假设您TextBox包含一个数字,如果数字为负,您希望它带有红色背景,如果 >= 0,则带有绿色背景

Here is the style you'd write:

这是您要编写的样式:

<TextBlock Text="{Binding Key}" >
  <TextBlock.Style>
    <Style TargetType="TextBox">
      <Setter Property="Background" Value="{Binding Key, Converter={StaticResource MyConverterResource}" />
    </Style>
  </TextBlock.Style>
</TextBlock>

Here is the converter you'd write:

这是您要编写的转换器:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double source = (double)value;
        return source < 0 ? Brushes.Red : Brushes.Green;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // We don't care about this one here
        throw new NotImplementedException();
    }
}

And, to access the converter in your xaml, you just have to do the following, assuming your converter is in the namespace MyNamespace:

而且,要访问您的 xaml 中的转换器,您只需执行以下操作,假设您的转换器位于命名空间中MyNamespace

<Window xmlns:my="clr-namespace:MyNamespace">
  <Window.Resources>
    <my:MyConverter x:Key="MyConverterResource">
  </Window.Resources?
  <!-- Your XAML here -->
</Window>

(of course you can put this in any Resources, may it be a UserControlor whatever ) This will allow you to call your converter by writing {StaticResource MyConverterResource}

(当然你可以把它放在 any 中Resources,可以是 aUserControl或其他)这将允许你通过写来调用你的转换器{StaticResource MyConverterResource}

And here, you'd have a conditional style, the converter deciding which color to set as a background according to one parameter, in my case, the value itself (but it can be whatever you want)

在这里,你有一个条件样式,转换器根据一个参数决定将哪种颜色设置为背景,在我的情况下,值本身(但它可以是任何你想要的)