具有2个不同的Observable集合绑定的按钮 [英] button with 2 different Observable collection binding

查看:70
本文介绍了具有2个不同的Observable集合绑定的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮作为datatemplate的列表框。



与ObservableCollection绑定的列表框可以说



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
   }





我需要绑定Settings集合的band1TextColor值。



< Button.Background>

< solidcolorbrush x:name =ButtonBrushxmlns:x =#unknown> Color =Violet />

< /Button.Background>





如何将按钮控件与2个不同的集合绑定?意味着按钮的内容由一个集合和背景颜色与另一个。




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 .

推荐答案





先让我们假设,有一个属性,如:



Hi,

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

public Settings Settings {get; set;}





DataContext中的某处 ListBox 控件的

此属性应定义在 TagValueOfPriority1 属性已定义。



然后你可以写:



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>





希望这有帮助,



Thomas。



Hope this helps,

Thomas.


这篇关于具有2个不同的Observable集合绑定的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆