2015-02-23 99 views
0

這是Windows Phone 8.1(運行時)Windows Phone CustomControl數據綁定

我有一些綁定自定義用戶controll與數據列表的問題。我會盡可能簡化。

我的問題是,如果我在自定義controll中使用DataBind {Binding Something},它將無法工作。

我需要將綁定數據(字符串)傳輸到自定義控件。

奇怪的是,如果我不使用DataBind,它將正常工作。例如MyCustomControllParameter =「一些字符串」(在我的示例'BindingTextValue'屬性中)

有誰知道如何將自定義用戶controll與DataTemplate內部的ListView綁定。

假設這樣的:

XAML試驗主頁

<Grid Background="Black"> 
    <ListView x:Name="TestList" Background="#FFEAEAEA"> 

     <ListView.ItemTemplate> 
      <DataTemplate> 
       <Grid Background="#FF727272"> 
        <local:TextBoxS BindingTextValue="{Binding Tag, FallbackValue='aSource'}" local:TextBoxS> 
       </Grid> 
      </DataTemplate> 
     </ListView.ItemTemplate> 



    </ListView> 

</Grid> 

XAML試驗主頁C#

public sealed partial class MainPage : Page 
{ 
    List<TTag> tags = new List<TTag>(); 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     this.NavigationCacheMode = NavigationCacheMode.Required; 
    } 


    public class TTag 
    { 
     public string Tag { get; set; } 
    } 

    private void InitializeAppData() 
    { 
     TTag tag = new TTag() { Tag = "hello world" }; 
     tags.Add(tag); 
     tags.Add(tag); 
     tags.Add(tag); 
     TestList.ItemsSource = tags; 
    } 

     protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 

     InitializeAppData(); 
    } 


} 

用戶控制XAML:

<UserControl 
x:Class="CustomControllTest.TextBoxS" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:CustomControllTest" 
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"> 

<Grid x:Name="LayoutRoot" Background="#FF4F4F4F" > 
    <RichTextBlock x:Name="MyTestBlock"> 
    </RichTextBlock> 
</Grid> 

用戶控制C#的.cs

public TextBoxS() 
    { 
     this.InitializeComponent(); 
     LayoutRoot.DataContext = this; 

    } 


    public static readonly DependencyProperty BindingTextValueProperty = DependencyProperty.Register(
            "BindingTextValue", 
            typeof(string), 
            typeof(TextBoxS), 
            new PropertyMetadata(default(string))); 

    public string BindingTextValue 
    { 
     get 
     { 
      return GetValue(BindingTextValueProperty) as string; 
     } 
     set 
     { 
      SetValue(BindingTextValueProperty, value); 
      //This method adds some custom logic into RichTextBlock, pointed correctly 
      SetupSpotterBox(value); 
     } 
    } 

感謝您的幫助;)

回答

2

你需要設置的的MainPage的DataContext因爲它不是某個位置設置過

public MainPage() 
{ 
    this.InitializeComponent(); 

    this.NavigationCacheMode = NavigationCacheMode.Required; 
    this.DataContext = this; 
} 

Next 從用戶控件刪除以下行

this.DataContext = this; 

這是爲了讓你的用戶控件拿起正確的DataContext例如MainPages

最後一些更改用戶控件的XAML

<UserControl 
x:Class="App21.TextBoxS" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:App21" 
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" x:Name="root"> 

<Grid x:Name="LayoutRoot"> 
    <TextBlock x:Name="MyTestBlock" FontSize="22" 
       Text="{Binding ElementName=root, Path=BindingTextValue}" Foreground="Red"> 
    </TextBlock>      
</Grid> 

注意控件本身的x:Name =「root」,並且我使用了一個TextBlock,以便我可以使用ElementName和Dependency Property BondingTextValue綁定到文本屬性。

+0

沒有probs不會忘記標記爲答案,如果它解決了問題或沒有,如果它沒有;) – SWilko 2015-02-24 13:40:26

0

您不必在屬性設置器中更新您的控件。試試這個代碼:

public static readonly DependencyProperty BindingTextValueProperty = DependencyProperty.Register(
           "BindingTextValue", 
           typeof(string), 
           typeof(TextBoxS), 
           new PropertyMetadata(default(string), PropertyChangedCallback)); 


private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { 

    //This method adds some custom logic into RichTextBlock, pointed correctly 
    var textBoxS = dependencyObject as TextBoxS; 

    textBoxS.SetupSpotterBox((string) args.NewValue); 
} 


public string BindingTextValue 
{ 
    get { return GetValue(BindingTextValueProperty) as string; } 
    set { SetValue(BindingTextValueProperty, value); } 
}