2011-03-20 33 views
2

UserControl具有3个依赖项属性:FormatAvailabilities,Orientation和FullText。 FormatAvailabilities被绑定到ItemsControl的ItemsSource属性。如果位于ItemsControl中ItemsPanelTemplate中的StackPanel方向绑定到Orientation属性。 FullText绑定到ItemsControl的DataTemplate中的两个TextBlocks的Visibility属性。我使用两个转换器来确定显示哪个TextBlock:一个BoolToVisibilityConverter和一个BoolToInvertedVisibilityConverter(后者是前者的反转)。我将TextBlock中的Visibility属性(两者都独立地)复制到ItemsControl中,并且它能正常工作。将ItemsControl ItemTemplate中的属性绑定到UserControl上的DP不起作用

似乎TextBlocks上的绑定工作不正常,因为两者都始终可见。由于它们都绑定在同一个属性上,但是一个是倒置的,所以两者都不应该同时可见。

我在我的转换器中放置了一个断点,它从未被击中,所以我的猜测是从重复控制内部绑定到它所在的外部控件时存在问题。

的App.xaml:

<common:BaseApp x:Class="xyz.App" xmlns:converters="clr-namespace:xyz.Converters;assembly=xyz"> 
    <common:BaseApp.RootVisual> 
     <phone:PhoneApplicationFrame x:Name="RootFrame" Source="/Home.xaml"/> 
    </common:BaseApp.RootVisual> 

    <common:BaseApp.Resources> 
     <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/> 
     <converters:BoolToVisibilityConverter x:Key="BoolToInvertedVisibilityConverter" IfTrue="Collapsed" IfFalse="Visible"/> 
    </common:BaseApp.Resources> 
</common:BaseApp> 

用户控件XAML:

<UserControl 
    x:Name="FormatsControl" 
    x:Class="xyz.Formats" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    d:DesignHeight="480" d:DesignWidth="480"> 

    <ItemsControl Background="Transparent" ItemsSource="{Binding ElementName=FormatsControl, Path=FormatAvailabilities}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="{Binding ElementName=FormatsControl, Path=Orientation}"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <TextBlock Text="{Binding BindsDirectlyToSource=True}" Margin="0,0,10,0" Visibility="{Binding ElementName=FormatsControl, Path=FullText, Converter={StaticResource BoolToVisibilityConverter}}"/> 
        <TextBlock Text="{Binding Description}" Margin="0,0,10,0" Visibility="{Binding ElementName=FormatsControl, Path=FullText, Converter={StaticResource BoolToInvertedVisibilityConverter}}"/> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</UserControl> 

用户控件CS:

namespace xyz 
{ 
    public partial class Formats : UserControl 
    { 
     public static readonly DependencyProperty FormatAvailabilitiesDependencyProperty = DependencyProperty.Register("FormatAvailabilities", typeof(FormatAvailability[]), typeof(Formats), null); 
     public static readonly DependencyProperty OrientationDependencyProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(Formats), new PropertyMetadata(Orientation.Horizontal)); 
     public static readonly DependencyProperty FullTextDependencyProperty = DependencyProperty.Register("FullText", typeof(bool), typeof(Formats), null); 

     public FormatAvailability[] FormatAvailabilities 
     { 
      get { return (FormatAvailability[])base.GetValue(Formats.FormatAvailabilitiesDependencyProperty); } 
      set { base.SetValue(Formats.FormatAvailabilitiesDependencyProperty, value); } 
     } 

     public Orientation Orientation 
     { 
      get { return (Orientation)base.GetValue(Formats.OrientationDependencyProperty); } 
      set { base.SetValue(Formats.OrientationDependencyProperty, value); } 
     } 

     public bool FullText 
     { 
      get { return (bool)base.GetValue(Formats.FullTextDependencyProperty); } 
      set { base.SetValue(Formats.FullTextDependencyProperty, value); } 
     } 

     public Formats() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

我必须俯瞰东西...谢谢!

+1

我假设你正在定义App.xaml或类似的资源BoolToVisibilityConverter和BoolToInvertedVisibilityConverter?这可能有助于用这个细节更新问题。 – 2011-03-20 19:33:56

+0

你的假设是正确的 - 更新我的问题,包括这一点。 – 2011-03-20 19:55:10

回答

1

没有与由本blog post,这也是目前在Windows Phone 7版本的Silverlight描述命名在Silverlight 3用户控件的问题。实际上,如果您在使用XAML的位置为UserControl提供了一个名称(即它是父级),那么它将覆盖UserControl自己的XAML文件中给出的名称。

+0

嗯,这是有道理的,我想。这是否意味着我没有办法让它工作,因为我已经发布了? – 2011-03-21 01:18:37

+0

@Josh - 事实上,这可能是另一个问题,因为快速测试表明它确实有效(不管文档说什么)。我更新了我的答案。 – CodeNaked 2011-03-21 11:48:31

+0

我以前遇到过这个 - 很烦人。 – 2011-03-27 15:15:11

-1

看起来像缺少OnPropertyChanged处理程序。

这是我的依赖属性之一。注意更改的处理程序。

public ObservableCollection<ObjWithDesc> ItemsSource 
{ 
    get 
    { 
     return (ObservableCollection<ObjWithDesc>)GetValue(ItemsSourceProperty); 
    } 
    set 
    { 
     SetValue(ItemsSourceProperty, value); 
    } 
} 

public static readonly DependencyProperty ItemsSourceProperty = 
    DependencyProperty.Register(
     "ItemsSource", 
     typeof(ObservableCollection<ObjWithDesc>), 
     typeof(HorizontalListBox), 
     new PropertyMetadata(OnItemsSourcePropertyChanged) 
    ); 

static void OnItemsSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
{ 
    ((HorizontalListBox) obj).OnItemsSourcePropertyChanged(e); 
} 

private void OnItemsSourcePropertyChanged(DependencyPropertyChangedEventArgs e) 
{ 
    ObservableCollection<ObjWithDesc> objWithDescList = (ObservableCollection<ObjWithDesc>)e.NewValue; 

    MainListBox.ItemsSource = objWithDescList; 
} 
+1

包含PropertyChangedHandler的PropertyMetaData是可选的。我没有对值做任何特殊的处理,所以不需要在PropertyChanged事件中做任何事情。这是一个直接和直接的约束。 – 2011-03-20 20:02:30

+0

值得一试,但看看它是否解决了这个问题?这听起来像你的依赖属性不工作时,一些东西改变 - 这应该解决这个问题。 – 2011-03-20 20:04:32

+0

我确实把事件联系起来了,我可以看到财产确实在变化。事实上,我将Visibility属性从TextBlock原样复制到ItemsControl,并且它在那里正常工作。 – 2011-03-20 20:14:58

1

我遇到了类似的问题,而不是结合的ElementName我改变了结合本

Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}} 

这工作得很好。

相关问题