2014-09-24 64 views
0

我在Windows Phone 8 Silverlight應用程序的每個頁面中都有一個標題。爲了不再重複自己,我決定創建一個簡單的用戶控件,它擁有一個網格和一個TextBlock。 TextBlock的文本綁定到UserControl的DependencyProperty(TitleProperty)。最後看代碼。綁定到UserControl DependencyProperty不能按預期工作

爲了使TitleProperty正常工作,我需要將UserControl的DataContext綁定到自身,代碼如下:DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"

使用頁面中的控制和設置固定值StaticBinding爲Title屬性時,該工作正常。例:

這工作:

<uiuc:SubSectionHeader Title="My fixed title" /> 

本工程以

<uiuc:SubSectionHeader Title="{Binding Source={StaticResource Literals}, Path=Literals.applicationSettings, FallbackValue='[Application Settings]'}" /> 

但做一個正常的綁定時它不工作。防爆

<uiuc:SubSectionHeader Title="{Binding Path=UserName'}" /> 

這是因爲用戶控件尋找自己的的DataContext(巫本身),而不是在頁面的DataContext 用戶名財產。

問題1是:在將值傳遞給Title屬性之前是否應該解決綁定問題?

問題2:難道僅僅是覆蓋在的用戶控件內部的約束力?

問題3:如何解決?請不要說創建一個自定義的控制,而不是因爲它吮吸

<UserControl x:Class="UI.UserControls.SubSectionHeader" 
      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" 
      xmlns:converters="clr-namespace:UI.Converters" 
      mc:Ignorable="d" 
      FontFamily="{StaticResource PhoneFontFamilyNormal}" 
      FontSize="{StaticResource PhoneFontSizeNormal}" 
      Foreground="{StaticResource PhoneForegroundBrush}" 
      d:DesignHeight="50" 
      d:DesignWidth="480" 
      DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"> 

    <UserControl.Resources> 
     <converters:StringCaseConverter x:Name="ToUppercase" /> 
     <converters:BooleanToVisibilityConverter x:Key="BoolToVisibility" /> 
    </UserControl.Resources> 

    <Grid x:Name="LayoutRoot" 
      Height="50" 
      Background="{Binding Source={StaticResource ThemeSetter}, Path=BackgroundColor}"> 

     <TextBlock VerticalAlignment="Center" 
        Margin="25 0 0 0" 
        Style="{StaticResource PhoneTextNormalStyle}" 
        Foreground="White" 
        Text="{Binding Path=Title, Mode=TwoWay, Converter={StaticResource ToUppercase}, ConverterParameter='upper', FallbackValue='[SUB SECTION TITLE]'}" /> 

    </Grid> 
</UserControl> 

後面的代碼:

using System.Windows; 
using System.Windows.Controls; 

namespace UI.UserControls { 

    public partial class SubSectionHeader : UserControl { 
     public SubSectionHeader() { 
      InitializeComponent(); 
     } 

     public string Title { 
      get { return (string)GetValue(TitleProperty); } 
      set { SetValue(TitleProperty, value); } 
     } 

     public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
      "Title", 
      typeof(string), 
      typeof(SubSectionHeader), 
      new PropertyMetadata("[SubSection Title]") 
     ); 

    } 
} 

回答

-1

你有沒有嘗試設置你的用戶控件的DataContext的後面的代碼? (你會刪除在XAML的setter)

public partial class SubSectionHeader : UserControl { 
     public SubSectionHeader() { 
      InitializeComponent(); 
      DataContext = this; 
     } 

編輯:嘗試將XAML內部控件的設計實例。 (將代碼保留在聲明中)

<UserControl 
    x:Class="MyControl 
    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" 
    d:DesignHeight="300" 
    d:DesignWidth="400" 
    d:DataContext="{d:DesignInstance}"> 
+0

是的,我有。這樣做破壞了Title屬性上的UserControl綁定。 – 2014-09-24 20:15:29

+0

我更新了我的答案。讓我知道這是否有幫助。 – Matthew 2014-09-25 11:37:54

+0

不,這沒有幫助。 d:DesignInstance適用於設計時間數據。我的問題不在設計時。 – 2014-09-25 11:45:17

相關問題