【问题标题】:Binding one dependency property to another将一个依赖属性绑定到另一个
【发布时间】:2012-06-22 16:54:30
【问题描述】:

我创建了一个自定义选项卡控件,但我遇到了问题。我有一个可编辑的文本框作为自定义 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 的依赖项。这个依赖属性是这个自定义选项卡控件的一部分。

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 道具中获取。我正在尝试的甚至可能吗?如果是这样,我做错了什么。

感谢收看。

【问题讨论】:

  • 我看不到任何带有PageTabControl 的x:Name 的东西,所以我可以想象ElementName="PageTabControl" 绑定会失败。另外,请问什么是从“Itemsource”中抓取它的内容,以及哪个“Itemsource”?

标签: wpf xaml dependency-properties


【解决方案1】:

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

具有名为 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>

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

【讨论】:

  • 虽然它是一个旧线程,但我遇到了同样的问题,诀窍是在用户控件内的绑定中精确定义 ElementName。可能有必要,因为不清楚当包含在另一个控件中时数据上下文可能是什么。在 Windows 包含中不需要 ElementName(第二个代码 sn-p)。谢谢你。
【解决方案2】:

我认为您已经为“SelectedIndex”属性解决了这个问题。对“CurrentPageType”属性做同样的事情,即使用RelativeSource

【讨论】:

    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多