How to bind dynamic resource key value to column value in data table in wpf

I am developing a custom control in wpf where I need to set the eclipse background color according to the value in the database. This field now contains values ​​from 1 to 6.

now I want my eclipse to have a different color according to the values ​​in this field. I have defined 6 different brushes in resources. Their key values ​​contain from 1 to 6 numbers.

now i know i can find bu key or name resources but don't want to. what i want, when i run the query according to the values ​​in the column, the dynamic resource value should be set. I don't want to do any processing, so I can directly bind the dynamic resource value ...

if you don't understand my question plz please indicate that I will put my code ...

+2


source to share


2 answers


If you have a value between 1 and 6 and you know what style should be for each, you should just set the style that has datatriggers for each value (1-6) and set any values ​​inside each trigger

<Window x:Class="WpfApplication8.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Style>
    <Style TargetType="{x:Type Window}">
        <Setter Property="Background" Value="Pink" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=textbox, Path=Text}" Value="1">
                <Setter Property="Background" Value="Green"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=textbox, Path=Text}" Value="2">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=textbox, Path=Text}" Value="3">
                <Setter Property="Background" Value="Blue"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=textbox, Path=Text}" Value="4">
                <Setter Property="Background" Value="Orange"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=textbox, Path=Text}" Value="5">
                <Setter Property="Background" Value="Indigo"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=textbox, Path=Text}" Value="6">
                <Setter Property="Background" Value="Violet"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Style>
<Grid Background="Transparent">
    <TextBox x:Name="textbox" Width="200" Height="30" />
</Grid>

      



+3


source


I think my ResourceKeyBinding extension can help you here. It allows you to use data binding to specify the resource key that you want to use.



+1


source







All Articles