簡體   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