C# 如何从自定义类中的其他类/控件访问属性?
我正在尝试复制windows Phone的消息传递系统,但使用的是另一项服务。 我正在使用来自的聊天气泡控件来执行此操作 (聊天气泡控件的屏幕截图): 下面的代码运行良好,但是datatemplate中的ChatBubbleDirection属性在数据绑定时会产生错误。这是因为我不知道如何使用另一个类中的属性(如果这有意义的话?)。如何做到这一点?我就是想不出来 XAML属性应如下所示:C# 如何从自定义类中的其他类/控件访问属性?,c#,windows-phone-7,class,xaml,toolkit,C#,Windows Phone 7,Class,Xaml,Toolkit,我正在尝试复制windows Phone的消息传递系统,但使用的是另一项服务。 我正在使用来自的聊天气泡控件来执行此操作 (聊天气泡控件的屏幕截图): 下面的代码运行良好,但是datatemplate中的ChatBubbleDirection属性在数据绑定时会产生错误。这是因为我不知道如何使用另一个类中的属性(如果这有意义的话?)。如何做到这一点?我就是想不出来 XAML属性应如下所示: ChatBubbleDirection="LowerLeft" 正如您所猜测的,这将决定聊天泡泡小箭头的
ChatBubbleDirection="LowerLeft"
正如您所猜测的,这将决定聊天泡泡小箭头的方向
private string direction;
public string Direction
{
get { return direction; }
set { direction = value; }
}
下面是message类的代码:
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类中写什么吗?我真的不知道如何进一步解释我的问题。我已经尝试在我的Message类中扩展Chatbuble类(如您所见),但没有任何效果
提前感谢您的帮助 您的消息类扩展了ChatBubble类,因此它已经具有父类的ChatBubbleDirection属性。你所需要写的就是:
Message m = new Message();
m.ChatBubbleDirection = ChatBubbleDirection.LowerRight;
您的消息类需要有一个名为Direction的公开属性,您将该属性绑定到ChatBubble的ChatBubbleDirection
private string direction;
public string Direction
{
get { return direction; }
set { direction = value; }
}
ChatBubbleDirection可以是以下内容之一
- 右上方
- 左上方
- 右下角
- 洛维夫特
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,
}
这在装订中不起作用+谢谢你的帮助!谢谢。看来我把事情复杂化太多了。。。一个简单的字符串就可以让它工作了!谢谢你,伙计:D