WPF DataTemplate DataContext 绑定问题
当使用DataTemplate时,需要用数据绑定,设置数据绑定的方式可参考:
<dxb:BarSubItem x:Name="brGroupBy" Content="Group By" ItemLinksSource="{Binding GroupByAttributes}" > <dxb:BarSubItem.ItemTemplate> <DataTemplate> <ContentControl > <dxb:BarButtonItem Content="{Binding DisplayName}" IsEnabled="{Binding Path=IsEnable}" > <DataTemplate> <dxb:BarButtonItem.ContentTemplate> <DataTemplate> <Grid HorizontalAlignment="Left"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBlock VerticalAlignment="Center" Text="{Binding}"> <Image x:Name="positionInformation" Grid.Column="1" Width="20" Height="15" Source="…/…/…/…/Resources/Images/Toast_Blue.png"> <Image.ToolTip> <ToolTip Style="{StaticResource ToolTipDisplayFormatStyle}" ToolTipService.ShowOnDisabled="True" Content="{Binding Reason}"/> </Image.ToolTip> <Image.Style> <Style> <Setter Property="Control.Visibility" Value="Collapsed" /> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding}" Value="Position"/> </MultiDataTrigger.Conditions> <Setter Property="Control.Visibility" Value="Visible" /> </MultiDataTrigger> </Style.Triggers> </Style> </Image.Style> </Image> </Grid> </DataTemplate> </dxb:BarButtonItem.ContentTemplate> </dxb:BarButtonItem> </ContentControl> </DataTemplate> </dxb:BarSubItem.ItemTemplate> </dxb:BarSubItem>
设置数据绑定属性的方式:
<dxb:BarButtonItem Content="{Binding}" ... -------- <TextBlock Text="{Binding DisplayName}" ... -------- <ToolTip Content="{Binding Reason}" ...
Content="{Binding}" 会将 GroupByAttribute 对象设置为ContentTemplate的DataContext;