views:

375

answers:

2

Hello, I am developing a user control in wpf where I need to set eclipse background color as per value in database. Now that field contains values between 1 to 6.

now I want that according to values in of that field my eclipse should have different color. I have defined 6 different brushes in resources. Their key values contain 1 to 6 number.

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

if you are not clear with my question plz specify i will put my code...

+1  A: 

I think my ResourceKeyBinding extension might be able to help you here. It lets you use databinding to specify the Key of the Resource that you want to use.

Samuel Jack
Unfortunately, this won't work. It's the KEY NAME that needs to be dynamic (not the resource itself).
senfo
@senfo: that's exactly what the ResourceKeyBinding does - lets you use databinding to specify the Key of the Resource.
Samuel Jack
My mistake...I must have read your answer wrong. I thought you were referring to a DynamicResource. http://msdn.microsoft.com/en-us/library/ms748942.aspxThank you for the information!
senfo
+2  A: 

If you have a value between 1 and 6 and you know what the style should be for each, you should just set a style that has datatriggers for each value (1-6) and set whatever 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>

JoshVarga
This actually worked beautifully and didn't require any code updates. Not to mention, it keeps the styling-specifics in the XAML (and out of the code) where they should be. Thank you very much for the post!By the way, I'm not the original author of this post, otherwise I'd accept this as the best answer. Not sure if somebody with higher reps than me can do that or not.
senfo
I'm glad it worked out for you.
JoshVarga
Since this worked out for you, could you mark this as the answer
JoshVarga