繁体   English   中英

在UserControl中获取TabItem名称

[英]Get TabItem Name in UserControl

我有以下代码创建一个TabControl。 每个选项卡都包含一个显示不同数据的UserControl(下面的代码)(一个显示本地税收信息,另一个显示美联储/州税收信息)。

TabControl

    <TabControl 
        Name="MappingTabs"
        Margin="6,7,7,8" Padding="6"
        Background="White" >
        <TabItem
            Name="LocalTaxTab"
            Padding="6,1"
            Header="Local">
            <AdornerDecorator>
                <DockPanel>
                    <Border Margin="7">
                        <GroupBox 
                            Name="LocalTaxesGroup">
                            <GroupBox.Header>
                                <TextBlock 
                                    FontWeight="Bold" 
                                    Text="Local Taxes">
                                </TextBlock>
                            </GroupBox.Header>
                            <StackPanel Margin="20,8,10,0"
                                        Orientation="Vertical">

                                <local:TaxCodeMappingHeader />

                                <!-- Note that a row is 25 high, -->
                                <ScrollViewer
                                        MaxHeight="250"
                                        >
                                    <ItemsControl  
                                            Name="LocalTaxCodeMappingControl"
                                            ItemTemplate="{StaticResource MappingRuleTemplate}" 
                                            BorderThickness="0" 
                                            AlternationCount="2" 
                                            IsTextSearchEnabled="False"
                                            HorizontalContentAlignment="Stretch"
                                            ItemsSource="{Binding TaxCodesCollection[0].CodeCollection, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}">
                                        <!--  ItemsSource="{Binding Source={StaticResource sortedCodeCollection}}">    -->
                                    </ItemsControl>
                                </ScrollViewer>

                                <local:TaxCodeMappingFooter DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"/>

                            </StackPanel>
                        </GroupBox>
                    </Border>
                </DockPanel>
            </AdornerDecorator>
        </TabItem>
        <TabItem
            Name="FedStateTaxesTab"
            Padding="6,1"
            Header="Federal\State">
            <AdornerDecorator>
                <DockPanel>
                    <Border Margin="7">
                        <GroupBox 
                            Name="FedStateTaxesGroup">
                            <GroupBox.Header>
                                <TextBlock 
                                    FontWeight="Bold" 
                                    Text="Federal \ State Taxes">
                                </TextBlock>
                            </GroupBox.Header>
                            <StackPanel Margin="20,8,10,0"
                                        Orientation="Vertical">

                                <local:TaxCodeMappingHeader />

                                <!-- Note that a row is 25 high, -->
                                <ScrollViewer
                                        MaxHeight="250"
                                        >
                                    <ItemsControl  
                                            Name="FedStateTaxCodeMappingControl"
                                            ItemTemplate="{StaticResource MappingRuleTemplate}" 
                                            BorderThickness="0" 
                                            AlternationCount="2" 
                                            IsTextSearchEnabled="False"
                                            HorizontalContentAlignment="Stretch"
                                            ItemsSource="{Binding TaxCodesCollection[1].CodeCollection, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}">
                                        <!--  ItemsSource="{Binding Source={StaticResource sortedCodeCollection}}">    -->
                                    </ItemsControl>
                                </ScrollViewer>

                                <local:TaxCodeMappingFooter DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"/>

                            </StackPanel>
                        </GroupBox>
                    </Border>
                </DockPanel>
            </AdornerDecorator>
        </TabItem>
    </TabControl>
</StackPanel>

UserControl (TaxCodeMappingFooter)

   <Button 
        Name="AddButton"
        Grid.Row="0"
        Grid.Column="0"
        Height="20" Width="20"
        Command="{Binding Path=DataContext.AddClickCommand}"
        CommandParameter="(want the tab name here)"
        Style="{StaticResource ImageButton}"
        ToolTip="Add a rule"
        local:AttachedImage.Image="{StaticResource AddImageSource}" />

UserControl(TaxCodeMappingFooter)包含一个Add按钮,我需要通过RelayCommand将其连接到VM。 我需要以某种方式告诉VM哪个选项卡正在调用Add命令,以便可以将项目添加到正确的集合中。 我考虑过发送TabName,然后将其禁用以了解用户所在的选项卡。

我的想法是正确的还是执行此操作的更好方法?如果正确,如何获取TabName值作为CommandParameter传递回去?

如果要像完成操作一样对UI控件进行硬编码,那么也许最简单的选择是在TaxCodeMappingFooter控件中定义string DependencyProperty

public static readonly DependencyProperty TabNameProperty = DependencyProperty.
    Register("TabName", typeof(string), typeof(TaxCodeMappingFooter));

public string TabName
{
    get { return (string)GetTabName(TabNameProperty); }
    set { SetTabName(TabNameProperty, value); }
}

然后可以从TabItem设置:

<local:TaxCodeMappingFooter TabName="FedStateTaxesTab" DataContext="{Binding 
    RelativeSource={RelativeSource AncestorType=UserControl}}" />

并从您的控件内部Bind到它:

<Button Name="AddButton" Command="{Binding Path=DataContext.AddClickCommand}" 
    CommandParameter="{Binding TabName, RelativeSource={RelativeSource 
    AncestorType=TaxCodeMappingFooter}}" ... />

正如其他人所说,如果您对视图模型结构进行适当的建模,那么这将不是什么大问题。

如果您确实想绑定祖先元素,则可以使用FindAncestorRelativeSource ,然后指定AncestorType 请注意,如果您是多个TabItem的后代,则可能需要调整AncestorLevel

{Binding Path=Name
         RelativeSource={RelativeSource Mode=FindAncestor,
                                        AncestorType={x:Type TabItem}}}

(为清楚起见添加了包装)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM