2012-06-22 54 views
0

我有我創建的自定義選項卡控件,但我遇到問題。我有一個可編輯文本框作爲自定義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。這個Depedency prop是這個自定義選項卡控件的一部分。

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中獲取內容,而不是從鏈接的依賴項中獲取內容。我正在嘗試甚至可能嗎?如果是這樣,我做錯了什麼。

感謝您的期待。

+0

我不能看到任何與'PageTabControl'的'x:Name',所以我可以想像'ElementName =「PageTabControl」'綁定將失敗。另外,我可以問什麼是從'Itemsource'中抓取其內容,以及'Itemsource'是什麼? –

回答

1

除非我失去了一些東西,這絕對有可能。這是一個簡化的工作示例。

用戶控制與名爲測試值的依賴性,含有與該屬性文本框:

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

這聽起來是問題出在您未發佈的代碼部分(例如,在內容綁定中使用了錯誤的名稱)。

0

我想你已經爲「SelectedIndex」屬性自己解決了這個問題。只是做「CurrentPageType」屬性相同的東西,即使用RelativeSource