2011-05-27 39 views
0

我不知道我在做什麼錯在這裏。我昨晚花了一個小時才弄明白,也許我只是啞巴。databinding上的一個用戶控件只能部分工作(silverlight)

我創建了此用戶控件以顯示邊框文本,該邊框文本使用數據綁定來填充樣式和文本。

這是我如何把它從主頁:

<mynamespace:BorderedText x:Name="DateTime" 
        Grid.Column="1" 
        Grid.Row="0" 
        BorderStyle="{StaticResource borderStyle}" 
        LabelStyle="{StaticResource labelStyle}" 
        TextStyle="{StaticResource valueStyle}" 
        Label="Current Date/Time"           
        Text="N/A" /> 

控制是非常簡單的:

<UserControl x:Class="MyNamespace.BorderedText" 
     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="480" 
     d:DesignWidth="480"> 

<Grid> 
    <Border Name="border" Style="{Binding BorderStyle}"> 
     <StackPanel> 
      <TextBlock Style="{Binding LabelStyle}" 
         Text="{Binding Label}" /> 
      <TextBlock Style="{Binding TextStyle}" 
         Text="{Binding Text}" /> 
     </StackPanel> 
    </Border> 
</Grid> 

的問題是,所有的數據綁定的作品,除了邊界數據綁定。我也嘗試將數據綁定到背景或任何其他屬性,但沒有成功。 後面的代碼設置了DependencyProperty屬性,就是這樣。請注意,用於數據綁定的DataContext在構造函數中設置。試圖將其分配給網格或邊界本身,但沒有成功。 有沒有人有任何線索或看到我在這裏失蹤的大東西?

namespace MyNamespace 
{ 
    public partial class BorderedText : UserControl 
    { 
     public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(BorderedText), new PropertyMetadata(null)); 

     public static readonly DependencyProperty LabelStyleProperty = DependencyProperty.Register("LabelStyle", typeof(Style), typeof(BorderedText), new PropertyMetadata(null)); 

     public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(BorderedText), new PropertyMetadata(null)); 

     public static readonly DependencyProperty TextStyleProperty = DependencyProperty.Register("TextStyle", typeof(Style), typeof(BorderedText), new PropertyMetadata(null)); 

     public static readonly DependencyProperty BorderStyleProperty = DependencyProperty.Register("BorderStyle", typeof(Style), typeof(BorderedText), new PropertyMetadata(null)); 

     public BorderedText() 
     { 
      InitializeComponent(); 
      ((Grid)this.Content).DataContext = this; 
      //((Border)this.Content).DataContext = this; 
     } 

     public string Label 
     { 
      set { SetValue(LabelProperty, value); } 
      get { return (string)GetValue(LabelProperty); } 
     } 

     public Style LabelStyle 
     { 
      set { SetValue(LabelStyleProperty, value); } 
      get { return (Style)GetValue(LabelStyleProperty); } 
     } 

     public string Text 
     { 
      set { SetValue(TextProperty, value); } 
      get { return (string)GetValue(TextProperty); } 
     } 

     public Style TextStyle 
     { 
      set { SetValue(TextStyleProperty, value); } 
      get { return (Style)GetValue(TextStyleProperty); } 
     } 

     public Style BorderStyle 
     { 
      set { SetValue(BorderStyleProperty, value); } 
      get { return (Style)GetValue(BorderStyleProperty); } 
     } 
    } 
} 

---- UPDATE:

它竟然是完全不同的東西和無關的數據綁定其正確佈線...

在邊框樣式我使用這個語法的背景屬性:

 <Setter Property="Background"> 
      <Setter.Value> 
       <SolidColorBrush> 
        <SolidColorBrush.Color> 
         <Color> 
          <Color.A> 
           100 
          </Color.A> 
          <Color.R>#95</Color.R> 
          <Color.B>#ED</Color.B> 
         </Color> 
        </SolidColorBrush.Color> 
       </SolidColorBrush> 
      </Setter.Value> 
     </Setter> 

這顯然是在設計師,但不是在電話。 將其更改爲:

 <Setter Property="Background"> 
      <Setter.Value> 
       <SolidColorBrush Color="#649500ED" /> 
      </Setter.Value> 
     </Setter> 

問題解決了

回答

0

好了,你忘了一件事......邊境的DataContext的! 給你的用戶控件的名稱,然後您可以添加到您的結合是這樣的:

<TextBox Text="{Binding Path=MyText, ElementName=UserControlRoot}" /> 

這會工作(至少它在WPF工作對我來說,嘿)

+0

邊框的DataContext的應在用戶控件的初始化器中定義(無論註釋還是未註釋的)。爲用戶控件賦予特定名稱不起作用,因爲我需要在調用頁面中多次重複使用usercontrol。如果我在usercontrol中給出了一個特定的名稱,那麼當我爲調用者中的控件分配一個x:Name時,數據綁定會再次中斷。 – 2011-05-28 00:49:18

+0

嗯,我不知道我跟着你.. 爲什麼它會在UserControl效果之外的任何東西之外呢?無論如何,如果你不想要一個名字,你可以將你的border/textblocks綁定到RelativeSource Find Ancestor類型的UserControl。 此外,初始化程序中的datacontext看起來不正確。你是說控制是一個網格,而不是。你可以做一些事情:DataContext = this; (我想,沒有測試) – Notter 2011-05-28 05:22:43

+0

如果你用命名外部的usercontrol,會發生什麼情況是該名稱會覆蓋分配給該對象的內部名稱。所以Binding ElementName =「nameassignedinternal」中斷。您可以在設計師身上看到這種情況。 – 2011-05-28 18:39:26

相關問題