1

我有一个已创建的自定义选项卡控件,但我遇到了问题。我有一个可编辑的文本框作为自定义 TabControl 视图的一部分。

<Controls:EditableTextControl x:Name="PageTypeName" 
                                  Style="{StaticResource ResourceKey={x:Type Controls:EditableTextControl}}" Grid.Row="0" TabIndex="0" 
                                  Uid="0"
                                  AutomationProperties.AutomationId="PageTypeNameTextBox"
                                  AutomationProperties.Name="PageTypeName"
                                  Visibility="{Binding ElementName=PageTabControl,Path=ShowPageType}">
        <Controls:EditableTextControl.ContextMenu>
            <ContextMenu x:Name="TabContextMenu">
                <MenuItem Header="Rename Page Type" Command="{Binding Path=PlacementTarget.EnterEditMode, RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
                          AutomationProperties.AutomationId="RenamePageTypeMenuItem"
                          AutomationProperties.Name="RenamePageType"/>
                <MenuItem Header="Delete Page Type" Command="{Binding Path=PageTypeDeletedCommand}" 
                          AutomationProperties.AutomationId="DeletePageTypeMenuItem"
                          AutomationProperties.Name="DeletePageType"/>
            </ContextMenu>
        </Controls:EditableTextControl.ContextMenu>
        <Controls:EditableTextControl.Content>
            <!--<Binding Path="CurrentPageTypeViewModel.Name" Mode="TwoWay"/>-->
            <Binding ElementName="PageTabControl" Path="CurrentPageTypeName" Mode ="TwoWay"/>
        </Controls:EditableTextControl.Content>
    </Controls:EditableTextControl>

在内容部分,我绑定到一个名为 CurrentPageTypeName 的 Dependency Prop。这个 Dependency 属性是这个自定义选项卡控件的一部分。

public static DependencyProperty CurrentPageTypeNameProperty = DependencyProperty.Register("CurrentPageTypeName", typeof(object), typeof(TabControlView));
    public object CurrentPageTypeName
    {
        get { return GetValue(CurrentPageTypeNameProperty) as object; }
        set { SetValue(CurrentPageTypeNameProperty, value); }
    }

在另一个视图中,我使用自定义 TabControl 然后将我的属性与实际名称值绑定到 CurrentPageTypeName 属性,如下所示:

 <Views:TabControlView Grid.Row="0" Name="RunPageTabControl" 
                          TabItemsSource="{Binding RunPageTypeViewModels}"                              
                          SelectedTab="{Binding Converter={StaticResource debugConverter}}" 
                          CurrentPageTypeName="{Binding Path=RunPageName, Mode=TwoWay}" 
                          TabContentTemplateSelector="{StaticResource tabItemTemplateSelector}"  
                          SelectedIndex="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SelectedTabIndex}"
                          ShowPageType="Hidden" >           
        <!--<Views:TabControlView.TabContentTemplate>
            <DataTemplate DataType="{x:Type ViewModels:RunPageTypeViewModel}">
                <RunViews:RunPageTypeView/>
            </DataTemplate>
        </Views:TabControlView.TabContentTemplate>-->

    </Views:TabControlView>

我的问题是似乎什么都没有发生。它是从 Itemsource 中获取其内容,而不是从我的链式 Dependency 道具中获取。我正在尝试的甚至可能吗?如果是这样,我做错了什么。

感谢您的关注。

4

2 回答 2

2

除非我遗漏了什么,否则这绝对是可能的。这是一个简化的工作示例。

具有名为 TestValue 的依赖属性的用户控件,其中包含绑定到此属性的 TextBox:

<UserControl x:Class="TestApp.TestControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
             x:Name="TestControlName">
  <Grid>
    <TextBox Text="{Binding ElementName=TestControlName, Path=TestValue, Mode=TwoWay}"/>
  </Grid>
</UserControl>

使用此用户控件的不同视图,将上述依赖项属性绑定到某些东西:

<Window x:Class="TestApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:TestApp="clr-namespace:TestApp" Title="MainWindow"
        Height="350" Width="525">
  <StackPanel>
    <TestApp:TestControl TestValue="{Binding ElementName=SourceTextBox, Path=Text, Mode=TwoWay}" />
    <TextBox Name="SourceTextBox" />
  </StackPanel>
</Window>

听起来问题出在您未发布的代码部分中(例如,内容绑定中使用了错误的名称)。

于 2012-06-22T21:13:56.720 回答
0

我认为您已经为 " SelectedIndex" 属性解决了这个问题。CurrentPageType只需对 " " 属性执行相同的操作,即使用RelativeSource

于 2012-06-22T21:16:07.700 回答