1:binding后台bool[]数据以及后台ObservableCollection数据
分别见下面xaml的Visibility和Text的Binding
public bool[] RubberTypeLegends { get { bool[] result = new bool[] {false,false,false }; for (int i = 0; i < ResultItems.Count; i++) { result[i] = true; } return result; } }
... <StackPanel x:Name="OSL_Legend" Margin="30,0,0,0" Orientation="Horizontal" Visibility="{Binding RubberTypeLegends[1], Converter={StaticResource boolToVisibilityConverter}}"> <Rectangle Width="10" Height="10" Fill="Blue" /> <TextBlock Margin="5,0,0,0" FontSize="9pt" Style="{StaticResource Univers57_Condensed}" Text="{Binding ResultItems[1].RubberType}" /> </StackPanel> ...
private ObservableCollection<OilSwellResultsItemViewModel> _resultItems; public ObservableCollection<OilSwellResultsItemViewModel> ResultItems { get { if (_resultItems == null) _resultItems = new ObservableCollection<OilSwellResultsItemViewModel>(); return _resultItems; } set { if (_resultItems != value) { _resultItems = value; this.RaisePropertyChanged("ResultItems"); } } }
public class OilSwellResultsItemViewModel : NotificationObject { ... private string _rubberType; public string RubberType { get { return _rubberType; } set { if (_rubberType != value) { _rubberType = value; this.RaisePropertyChanged("RubberType"); } } } ... }
2:binding wpf control自己的属性
注意Binding="{Binding Items.Count, ElementName=DataGrid}"的ElementName是指向DataGrid
<ControlTemplate.Triggers> <DataTrigger Binding="{Binding Items.Count, ElementName=DataGrid}" Value="1"> <Setter TargetName="OS_Legend" Property="Visibility" Value="Visible" /> <Setter TargetName="OSL_Legend" Property="Visibility" Value="Collapsed" /> <Setter TargetName="OSLSR_Legend" Property="Visibility" Value="Collapsed" /> </DataTrigger> <DataTrigger Binding="{Binding Items.Count, ElementName=DataGrid}" Value="2"> <Setter TargetName="OS_Legend" Property="Visibility" Value="Visible" /> <Setter TargetName="OSL_Legend" Property="Visibility" Value="Visible" /> <Setter TargetName="OSLSR_Legend" Property="Visibility" Value="Collapsed" /> </DataTrigger> <DataTrigger Binding="{Binding Items.Count, ElementName=DataGrid}" Value="3"> <Setter TargetName="OS_Legend" Property="Visibility" Value="Visible" /> <Setter TargetName="OSL_Legend" Property="Visibility" Value="Visible" /> <Setter TargetName="OSLSR_Legend" Property="Visibility" Value="Visible" /> </DataTrigger> </ControlTemplate.Triggers>