【问题标题】:prism - Unable to set binding for HeaderTemplate of TabItemprism - 无法为 TabItem 的 HeaderTemplate 设置绑定
【发布时间】:2013-07-04 04:17:05
【问题描述】:

我有一个名为 ProductEditView 的用户控件,我将它添加到我的 MainWindow (shell) 的一个区域中。我在 Shell 区域中有一个 TabControl 来显示 ProductEditView,如下所示

<TabControl Name="tabEditProduct" 
            prism:RegionManager.RegionName="TabContent"

    <TabControl.ItemContainerStyle>
    <Style TargetType="TabItem">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <DockPanel LastChildFill="True" >
                        <Button Width="16" Height="16" Command="{Binding Path=CloseTabCommand}" 
                                        DockPanel.Dock="Right" BorderThickness="0" Padding="0"
                                        BorderBrush="Transparent" Background="Transparent">
                            <Image Source="Images/close-icon.png" Width="14" Height="14" 
                                           HorizontalAlignment="Stretch" Margin="0"/>
                        </Button>
                        <TextBlock Text="{Binding ProductName}" 
                                           DockPanel.Dock="Left" Margin="0,0,10,0" />
                    </DockPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

在 TabItem 的 HeaderTemplate 中,我尝试使用绑定显示来自产品对象的文本。不幸的是绑定不起作用,但我可以将静态文本显示为标题。 是否可以像上面那样使用绑定来显示选项卡项的标题文本?

为了完整起见,我还将视图粘贴到作为 TabItem 加载到 TabControl 区域中的 Edit Product。其绑定运行良好。这是 ProductEditView

<UserControl x:Class="TextExisting.CoreModule.ProductEditView"
....

        <Grid HorizontalAlignment="Center" DataContext="{Binding Path=Product}" Grid.Row="0" Grid.Column="0">
        ....
            <TextBlock Text="Id" Grid.Row="0" Grid.Column="0"/>
            <TextBox Grid.Row="0" Grid.Column="1" 
                     Text="{Binding Path=Id, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />

            <TextBlock Text="Product Name" Grid.Row="1" Grid.Column="0"/>
            <TextBox Grid.Row="1" Grid.Column="1" 
                     Text="{Binding Path=ProductName, NotifyOnValidationError=True, ValidatesOnExceptions=True}" />
            ....
        </Grid>
        <Button Command="{Binding SaveProductCommand}" Content="Save Product" HorizontalAlignment="Center"/>

</UserControl>

【问题讨论】:

    标签: wpf prism prism-4


    【解决方案1】:

    也许可以添加这样的东西 "{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Path=DataContext.ProductName}"

    这将向上移动并找到带有绑定到您的视图模型及其属性的用户控件的数据上下文。

    这是我使用的小备忘单WPF Cheat Sheet

    【讨论】:

      【解决方案2】:

      Here 是解释如何在这种情况下绑定的链接。

      【讨论】:

      • 您的链接已损坏,这正是 Stack Overflow 不欢迎仅链接答案的原因(尽管当您发布答案时可能并不那么不受欢迎)。
      • 这是链接,解决方案对我有用blog.vivekmehta.com/2011/06/…
      • 两个链接都坏了
      猜你喜欢
      • 2011-11-21
      • 2023-04-02
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      相关资源
      最近更新 更多