How can I bind dynamic resource key value with column value in data table in wpf
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 defi开发者_Go百科ned 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...
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>
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.
精彩评论