2012-07-31 44 views
1

我試圖複製windows手機的消息系統,但與另一項服務。 我使用Coding4Fun Toolkit for Windows Phone的Chat Bubble控件來執行此操作。如何從自定義類中訪問另一個類/控件的屬性?

(聊天泡泡的截圖控制):

enter image description here

我想出了下面的代碼工作正常,但我的DataTemplate內ChatBubbleDirection財產時,它的數據綁定產生一個錯誤。這是因爲我不知道如何使用其他類的屬性(如果這有意義的話)。這將如何完成?我只是想不通......

的XAML屬性應該是這樣的:

ChatBubbleDirection="LowerLeft" 

正如您可以猜到,這將決定ChatBubble的小箭頭的方向。

下面是消息類代碼:

using Coding4Fun.Phone.Controls.Toolkit.Common; 

public class Message : Coding4Fun.Phone.Controls.ChatBubble 
{ 
    public string Text { get; set; } 
    public string SendingDate { get; set; } 
    //public Coding4Fun.Phone.Controls.ChatBubble { get; set; } 
} 

而這裏的按鈕單擊事件中的代碼:

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     LBmessages.ItemsSource = messages; 
     Message m = new Message(); 
     m.SendingDate = "Today"; 
     m.Text = "This is a message"; 
     //m.direction = (Coding4Fun.Phone.Controls.ChatBubble).ChatBubbleDirectionProperty??? 
     messages.Add(m); 
    } 

而這裏的XAML:

<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <ScrollViewer Height="369" Name="scrollviewer1" Width="500"> 
      <ListBox Name="LBmessages" Height="250"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Width="456"> 
          <cc:ChatBubble Width="500" Margin="0,0,0,20" ChatBubbleDirection="{Binding direction}"> 
          <Grid> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="Auto"></RowDefinition> 
            <RowDefinition Height="40"></RowDefinition> 
           </Grid.RowDefinitions> 
           <TextBlock Text="{Binding Text}" TextWrapping="Wrap" Width="430"></TextBlock> 
           <TextBlock Grid.Row="1" HorizontalAlignment="Right" Text="{Binding SendingDate}"></TextBlock> 
          </Grid> 
         </cc:ChatBubble> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     </ScrollViewer> 
    </StackPanel> 

有誰有什麼想法,我應該寫在消息類?我真的不知道如何進一步解釋我的問題。我已經嘗試在我的Message類中擴展ChatBubble類(如您所見),但無濟於事。

在此先感謝您的幫助!

回答

1

您的郵件類需要有一個公開曝光性質叫的方向,你會綁定到ChatBubble的ChatBubbleDirection。

private string direction; 

public string Direction 
{ 
    get { return direction; } 
    set { direction = value; } 
} 

ChatBubbleDirection可以是下列

  • UpperRight
  • UpperLeft
  • LowerRight
  • LowerLeft

這應該工作,我覺得之一。

更多信息可在WindowsPhoneGeek找到。

+0

看起來我太過於複雜的東西了......一個簡單的字符串就是它讓它工作所需的一切!謝謝老兄:D – 2012-07-31 16:43:50

1

您的消息類擴展了ChatBubble類,所以它已經具有父類的ChatBubbleDirection屬性。所有你需要寫的是:

Message m = new Message(); 
m.ChatBubbleDirection = ChatBubbleDirection.LowerRight; 
+0

這並沒有在綁定工作。無論如何幫助+1!謝謝。 – 2012-07-31 16:43:02

0

public ChatBubbleDirection _Direction; 

    public ChatBubbleDirection Direction 
    { 
     get 
     { 
      return _Direction; 
     } 
     set 
     { 
      if (value != _Direction) 
      { 
       _Direction = value; 
       NotifyPropertyChanged("Direction"); 
      } 
     } 
    } 

類主代碼

BBChat.Items.Add(new BMessage() 
{ 
Direction = ChatBubbleDirection.UpperLeft, 
} 
相關問題