2011-03-20 67 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}} 

這工作得很好。

相關問題