Click here to Skip to main content
14,734,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a List box with Buttons as datatemplate .

Listbox which binds with ObservableCollection lets say

<ListBox Height="84" HorizontalAlignment="Left" Margin="24,12,0,0" Name="lstband1" VerticalAlignment="Top" Width="854" ItemsSource="{Binding Path=TagValueOfPriority1}"  Style="{DynamicResource ListBoxStyle}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"/>


With Style

<Style TargetType="ListBoxItem" x:Key="SimpleListBoxItem">
            <Setter Property="Padding" Value="0 0 0 0"/>
           
        </Style>
       
            <Style TargetType="ListBox" x:Key="ListBoxStyle">
            <Setter Property="ItemContainerStyle" Value="{StaticResource SimpleListBoxItem}"/>
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="Configuration" Command="{Binding ButtonCommand}" CommandParameter="Alarm"/>

                    </ContextMenu>
                </Setter.Value>
            </Setter>
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            
                        <Grid VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="1">
                            <Button Name ="Button" Content="{Binding Path=TagDescription}" 
                            Height="70" 
                            Width="100"
                            FontSize="12" 
                            FontWeight="Bold"
                            HorizontalAlignment="Stretch"
                                    
                            Command="{Binding Path=DataContext.GetChildrenCommand}" 
                            CommandParameter="{Binding}" >
                                <Button.Background>

                                    <SolidColorBrush x:Name="ButtonBrush"  Color="Violet" />
                                </Button.Background>
                                
                            </Button>
                        </Grid>
                        <DataTemplate.Triggers>
                            <EventTrigger RoutedEvent="FrameworkElement.Loaded" SourceName="Button">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="ButtonBrush"  To="Red"  Storyboard.TargetProperty="Color"
                                     RepeatBehavior="Forever"
                                     AutoReverse="True"
                                     Duration="0:0:0.5"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </DataTemplate.Triggers>    
                    </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>


I have another observable collection with settings details lets say

class Settings
   {
       private Color mBand1_TextColor;

       private Boolean mContinuous;

       public Color band1TextColor
       {
            get
           {
               return mBand1_TextColor;
           }
           set
           {
               mBand1_TextColor = value;
               OnPropertyChanged("Band1TextColor");
           }
       }
       public Boolean Continuous
       {
           get
           {
               return mContinuous;
           }
           set
           {
               mContinuous = value;
               OnPropertyChanged("Continuous");
           }
       }
       #region INotifyPropertyChanged Members

       public event PropertyChangedEventHandler PropertyChanged;
       private void OnPropertyChanged(string propertyName)
       {
           if (PropertyChanged != null)
           {
               PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
           }
       }
       #endregion
   }



inside the Style of listbox for Button background , i need to bind the Settings collections's band1TextColor value .

<Button.Background>
<solidcolorbrush x:name="ButtonBrush" xmlns:x="#unknown">Color="Violet" />
</Button.Background>


How to bind the button control with 2 different collections?Means Content of button is filled by one collection and background color with another .
Posted

1 solution

Hi,

first let's assume, there is a property like:

public Settings Settings {get; set;}


somewhere in the DataContext of the ListBox control.
This property should be defined where the TagValueOfPriority1 property is defined.

Then you could write:
<Button.Background>
  <SolidColorBrush x:Name="ButtonBrush"  Color="{Binding Path=DataContext.Settings.band1TextColor", RelativeSource={RelativeSource AncestorType=ListBox, Mode=FindAncestor}} />
</Button.Background>


Hope this helps,

Thomas.
   
Comments
Member 8786335 28-Mar-13 6:36am
   
List Box is like

<ListBox Height="84" HorizontalAlignment="Left" Margin="24,12,0,0" Name="lstband1" VerticalAlignment="Top" Width="854" ItemsSource="{Binding Path=TagValueOfPriority1}" Style="{DynamicResource ListBoxStyle}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"/>

How to add the Settings here ?
Thomas Duwe 28-Mar-13 6:49am
   
You don't have to add the Settings to the ListBox. As I said, you need a property named Settings in the DataContext of the ListBox,
where the TagValueOfPriority1 property is defined.
Member 8786335 28-Mar-13 8:34am
   
inside the style of Listbox i have added
setter property="DataContext" value="{Binding Path=Settings}"

<Button.Background>

And used it in
solidcolorbrush x:name="ButtonBrush" color="{Binding Path=DataContext.Settings.band1TextColor, RelativeSource={RelativeSource AncestorType=ListBox, Mode=FindAncestor}}"

But not worked out .
Thomas Duwe 28-Mar-13 10:04am
   
Of course this can't work.
If you set the DataContext of the ListBox to Settings, then the Binding Path=DataContext.Settings....
is wrong and it should be Binding Path=DataContext.band1TextColor.

And your setting of the DataContext of the ListBox was not what I said you should do.

Obviously you need to study a little bit more about DataBinding and XAML/code behind and what the DataContext really is.

As I said, you should define a property public Settings Settings {get; set;} in the object, that is the DataContext of your ListBox.
The DataContext of your ListBox is usually an instance of a class.
Where did you define the property TagValueOfPriority1?
There you should define the property for Settings, because your binding of the ItemsSource of the ListBox is a short version of the binding:
{Binding Path=DataContext.TagValueOfPriority1}.
So whereever you defined the property of TagValueOfPriority1 you should define the property for Settings and assign a valid instance to it.
Member 8786335 29-Mar-13 5:14am
   
iam new to WPF .Now only learning it.

ListBox Height="84" HorizontalAlignment="Left" Margin="24,12,0,0" Name="lstband1" VerticalAlignment="Top" Width="854" ItemsSource="{Binding Path=TagValueOfPriority1}" DataContext="{Binding Path=Settings}" Style="{DynamicResource ListBoxStyle}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"

And Background of Button i tried with Color="{Binding Path=DataContext.band1TextColor", RelativeSource={RelativeSource AncestorType=ListBox, Mode=FindAncestor}}


Now the Listbox is not performing any binding even TagPriorityOfValue1 is not working now .
Thomas Duwe 1-Apr-13 11:41am
   
The ListBox cannot perform the binding to TagPriorityOfValue1 because you changed the DataContext of the ListBox to the instance of the Settings class and this class doesn't contain a property TagPriorityOfValue1.

If you want further help then post the class which contains the TagPriorityOfValue1 property and I will show you what to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900