Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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# 是否可以将参数(绑定)传递给WPF样式_C#_Wpf_Xaml_Styles_Controltemplate - Fatal编程技术网

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

C# 是否可以将参数(绑定)传递给WPF样式,c#,wpf,xaml,styles,controltemplate,C#,Wpf,Xaml,Styles,Controltemplate,我将WPF样式应用于列表框项目。当前,每个项都绑定到一个KeyValuePair。我在ListBoxItem内的文本块中显示键: <TextBlock Name="Label" Text="{Binding Key}" /> 我想做的是使样式通用,这样如果数据不是KeyValuePair(可能只是字符串),我就可以将数据正确绑定到TextBlock 是否有方法将参数传递给样式或数据模板或使数据绑定通用? 我的风格: <Style x:Key="ListBoxItemSty

我将WPF样式应用于列表框项目。当前,每个项都绑定到一个KeyValuePair。我在ListBoxItem内的文本块中显示键:

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

我想做的是使样式通用,这样如果数据不是KeyValuePair(可能只是字符串),我就可以将数据正确绑定到TextBlock

是否有方法将参数传递给样式或数据模板或使数据绑定通用?

我的风格:

<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>

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

以下是您的写作风格:

<TextBlock Text="{Binding Key}" >
  <TextBlock.Style>
    <Style TargetType="TextBox">
      <Setter Property="Background" Value="{Binding Key, Converter={StaticResource MyConverterResource}" />
    </Style>
  </TextBlock.Style>
</TextBlock>
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();
    }
}
(当然,您可以将其放在任何
资源中
,可能是
用户控件
或其他任何内容) 这将允许您通过编写
{StaticResource MyConverterResource}


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

使用转换器怎么样?你试过了吗?我还没有试过用转换器,但那可能行得通。我愿意接受任何让这项工作发挥作用的建议。
<Window xmlns:my="clr-namespace:MyNamespace">
  <Window.Resources>
    <my:MyConverter x:Key="MyConverterResource">
  </Window.Resources?
  <!-- Your XAML here -->
</Window>