Home > Blockchain >  WPF - Pass value as parameter to a style
WPF - Pass value as parameter to a style

Time:11-18

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.

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 "???" 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.

Is there a solution to this?

CodePudding user response:

You can put it in an ItemsControl and bind a list of strings.

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:

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

CodePudding user response:

You could use a DynamicResource also for the 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>
  • Related