简体   繁体   English

WPF - 将值作为参数传递给样式

[英]WPF - Pass value as parameter to a style

I have two TextBoxes with the same properties.我有两个具有相同属性的文本框。 For instance:例如:

<!-- TextBox1 -->
<TextBox Foreground="Red"
         Style="{StaticResource MaterialTextBox}"
         materialDesign:HintAssist.Foreground="Blue"
         materialDesign:HintAssist.HintOpacity="100"
         materialDesign:TextFieldAssist.UnderlineBrush="Green">
    <materialDesign:HintAssist.Hint>
        <TextBlock Foreground="{DynamicResource Lime}"
                   Text="SomeHint1" />
    </materialDesign:HintAssist.Hint>
</TextBox>

<!-- TextBox2 -->
<TextBox Foreground="Red"
         Style="{StaticResource MaterialTextBox}"
         materialDesign:HintAssist.Foreground="Blue"
         materialDesign:HintAssist.HintOpacity="100"
         materialDesign:TextFieldAssist.UnderlineBrush="Green">
    <materialDesign:HintAssist.Hint>
        <TextBlock Foreground="{DynamicResource Lime}"
                   Text="SomeHint2" />
    </materialDesign:HintAssist.Hint>
</TextBox>

Notice the only difference is the "SomeHint1" and "SomeHint2" strings for the MaterialDesign:HintAssist.Hint property.请注意,唯一的区别是MaterialDesign:HintAssist.Hint属性的"SomeHint1""SomeHint2"字符串。

So I thought I could make a style for this:所以我想我可以为此设计一种风格:

<Style TargetType="TextBox" x:Key="TextBoxWithMaterialHintStyle" BasedOn="{StaticResource MaterialTextBox}">
    <Setter Property="Foreground" Value="Red" />
    <Setter Property="materialDesign:HintAssist.Foreground" Value="Blue" />
    <Setter Property="materialDesign:HintAssist.HintOpacity" Value="100" />
    <Setter Property="materialDesign:TextFieldAssist.UnderlineBrush" Value="Green" />
    <Setter Property="materialDesign:HintAssist.Hint">
        <Setter.Value>
            <TextBlock Foreground="{DynamicResource Blue}"
                       Text="???" />
        </Setter.Value>
    </Setter>
</Style>

Notice the TextBlock's Text property is "???"注意 TextBlock 的Text属性是"???" in my style.以我的风格。
I don't know a way to pass either "SomeHint1" or "SomeHint2" to the Style and further to the TextBlock's Text property.我不知道将"SomeHint1""SomeHint2"传递给 Style 并进一步传递给 TextBlock 的Text属性的方法。

Is there a solution to this?有针对这个的解决方法吗?

You could use a DynamicResource also for the Text您也可以将DynamicResource用于Text

<Setter.Value>
   <TextBlock Foreground="{DynamicResource Blue}" Text="{DynamicResource hintStr}"/>
</Setter.Value>

and then just然后就

<TextBox ...>
   <TextBox.Resources>
      <Sys:String x:Key="hintStr" xmlns:Sys="clr-namespace:System;assembly=mscorlib">Hint1</Sys:String>
   </TextBox.Resources>
</TextBox>

You can put it in an ItemsControl and bind a list of strings.您可以将它放在 ItemsControl 中并绑定字符串列表。

For example:例如:

<ItemsControl ItemsSource="{Binding Hints}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <TextBox Foreground="Red"
                Style="{StaticResource MaterialTextBox}"
                materialDesign:HintAssist.Foreground="Blue"
                materialDesign:HintAssist.HintOpacity="100"
                materialDesign:TextFieldAssist.UnderlineBrush="Green">
            <materialDesign:HintAssist.Hint>
                <TextBlock Foreground="{DynamicResource Lime}"
                           Text="{Binding}" />
            </materialDesign:HintAssist.Hint>
        </TextBox>
    </ItemsControl.ItemTemplate>
</ItemsControl>

In your ViewModel can be a property like this:在您的 ViewModel 中可以是这样的属性:

public string[] Hints
{
     get { return new[] {"SomeHint1", "SomeHint2"}; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM