- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有 View 模型
public class MessengerViewModel : INotifyBaseClass
{
public ServiceManager ServiceManager { get; set; }
private ObservableCollection<Conversation> _conversations;
private Conversation _currentConversation;
private MessagesPage _parent;
private List<Conversation> _tempList;
private bool _canClear;
private bool _isSearching;
private Random _random;
public ObservableCollection<Conversation> Conversations
{
get { return _conversations; }
set { SetField(ref _conversations, value); }
}
public Conversation CurrentConversation
{
get { return _currentConversation; }
set
{
_parent.MessagesListBox.ItemsSource = value.Messages;
if (_tempList != null)
{
if (_tempList.Contains(value))
{
_tempList.Remove(value);
}
ClearSearchUsers();
_parent.ConversationsListBox.Items.Refresh();
}
SetField(ref _currentConversation, value);
}
}
...
}
对话准则
public class Conversation : INotifyBaseClass
{
private string _interlocutorId;
private ObservableCollection<Message> _messages;
private string _name;
private string _username;
private Brush _brush;
private bool _isActive;
public bool IsActive
{
get { return _isActive; }
set { SetField(ref _isActive, value); }
}
public string InterlocutorId
{
get { return _interlocutorId; }
set { SetField(ref _interlocutorId, value); }
}
public string Name
{
get { return _name; }
set { SetField(ref _name, value); }
}
public Brush Color
{
get { return _brush; }
set { SetField(ref _brush, value); }
}
public string ShortName
{
get
{
var arr = _name.Split(' ');
if (arr.Length == 1)
return arr[0].ToUpper()[0].ToString();
return arr.Aggregate((x, y) => $"{x[0]}{y[0]}").ToUpper();
}
}
public string Username
{
get { return _username; }
set { SetField(ref _username, value); }
}
public ObservableCollection<Message> Messages
{
get { return _messages; }
set { SetField(ref _messages, value); }
}
}
我的 Xaml 代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="260"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*" MinWidth="260"/>
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1" x:Name="InterfaceSplitter" HorizontalAlignment="Center" Width="3"/>
<Grid Grid.Column="0" x:Name="ConversationsGrid">
<Grid.RowDefinitions>
<RowDefinition Height="65"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid x:Name="SearchAndMenuGrid" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="../Resources/menuIcon.png" Width="40" VerticalAlignment="Center"/>
<TextBox x:Name="SearchTextBox" Grid.Column="1" Height="30" Margin="15" FontFamily="Arial" VerticalContentAlignment="Center" TextChanged="SearchTextBox_TextChanged"/>
</Grid>
<ListBox x:Name="ConversationsListBox" Grid.Row="1" ItemsSource="{Binding Conversations}" SelectedItem="{Binding CurrentConversation}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5" Height="55" Orientation="Horizontal" ClipToBounds="True">
<Border Background="{Binding Color}" Width="40" Height="40" VerticalAlignment="Center">
<Border.Clip>
<EllipseGeometry RadiusX="20" RadiusY="20" Center="20,20"/>
</Border.Clip>
<TextBlock FontSize="18" Text="{Binding Path=ShortName}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<TextBlock FontSize="18" Text="{Binding Path=Name}" Margin="15,2.5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
<Grid Grid.Column="2" x:Name="MessagesGrid">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="ConversationPropertiesGrid" Background="White">
<TextBlock Text="{Binding Path = CurrentConversation.Name, Converter={StaticResource DebugDummyConverter}}"/></Grid>
<ListBox x:Name="MessagesListBox" Grid.Row="1" Background="Transparent" HorizontalContentAlignment="Stretch" ItemsSource="{Binding CurrentConversation.Messages}" ItemContainerStyle="{StaticResource RemoveSelectionStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,5">
<Border x:Name="MessageBorder" CornerRadius="20">
<TextBlock x:Name="MessageTextBlock" FontSize="18" Text="{Binding Path = Content,PresentationTraceSources.TraceLevel=High}" HorizontalAlignment="Right" Margin="10,15"/>
</Border>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsAuthor}" Value="true">
<Setter TargetName="MessageBorder" Property="Background" Value="White"/>
<Setter TargetName="MessageBorder" Property="HorizontalAlignment" Value="Right"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsAuthor}" Value="false">
<Setter TargetName="MessageBorder" Property="Background" Value="Lime"/>
<Setter TargetName="MessageBorder" Property="HorizontalAlignment" Value="Left"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Row="2" x:Name="SendMessageGrid" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Name="MessageTextBox" Grid.Column="0" BorderThickness="0" Margin="10,10" VerticalContentAlignment="Center" AcceptsReturn="True" PreviewKeyDown="MessageTextBox_PreviewKeyDown"/>
</Grid>
</Grid>
</Grid>
问题是当我尝试在 MessageListBox 或 TextBlock {Binding CurrentConversation.Property} 中使用 Binding 时,绑定(bind)失败并显示
System.Windows.Data Warning: 67 : BindingExpression (hash=8769005): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=8769005): Found data context element: TextBlock (hash=46431654) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=8769005): Activate with root item MessengerViewModel (hash=32244734)
System.Windows.Data Warning: 107 : BindingExpression (hash=8769005): At level 0 using cached accessor for MessengerViewModel.CurrentConversation: RuntimePropertyInfo(CurrentConversation)
System.Windows.Data Warning: 104 : BindingExpression (hash=8769005): Replace item at level 0 with MessengerViewModel (hash=32244734), using accessor RuntimePropertyInfo(CurrentConversation)
System.Windows.Data Warning: 101 : BindingExpression (hash=8769005): GetValue at level 0 from MessengerViewModel (hash=32244734) using RuntimePropertyInfo(CurrentConversation): <null>
System.Windows.Data Warning: 106 : BindingExpression (hash=8769005): Item at level 1 is null - no accessor
System.Windows.Data Warning: 80 : BindingExpression (hash=8769005): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 88 : BindingExpression (hash=8769005): TransferValue - using fallback/default value ''
System.Windows.Data Warning: 89 : BindingExpression (hash=8769005): TransferValue - using final value ''
但 CurrentConversation 的所有属性都已启动。在开始时,CurrentConversation 为空,直到用户在 ConversationsListBox 中选择它。我想这可能是个问题,但我添加了 _currentConversation = new Conversation() 并且绑定(bind)仍然不起作用。
更新
我的 INotifyBaseClass:
public class INotifyBaseClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;
field = value;
OnPropertyChanged();
return true;
}
}
最佳答案
在每个 setter 中添加,""
(SetField
)
例子:
public bool IsActive
{
get { return _isActive; }
set { SetField(ref _isActive, value, ""); }
}
如果类中有这样一个函数:INotifyBaseClass
protected bool SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
关于c# - Wpf 绑定(bind)到对象属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875539/
有没有办法在另一个 WPF 窗口内托管 WPF 窗口。我有几个有点复杂的表格。但现在为了简化事情,我试图将其中一些合并为一个“仪表板”表单中的标签页。 请注意,我不是要托管 Windows 窗体,而是
WPF 特有的哪些方面和实践在非 WPF GUI 编程中最有用(并且并非难以实现)? 最佳答案 通过学习 WPF 命令,我了解了命令模式。它构成了 UI - 代码分离的基础,我认为应该在其他应用程序中
WinRT/Metro 正在获得一个新的 SemanticZoom控制,但我很难为 WPF 找到任何东西。 我不想为我的特定项目切换到 Metro,因为它不允许我制作窗口应用程序或跨多个显示器的多个实
我很难解决我的问题,我快要疯了。 想法是这样的:我有两个 ListView 元素,当一个元素从第一个列表掉落到第二个列表时,我需要打开一个对话,但我需要被掉落的元素的信息以及被添加以填充对话的元素。
如果我遵循TabControl,并且一切正常,当我切换到第二个Tabitem时,它显示就没有问题。 //datagrid //datagrid2 但是如果我有这个xaml,当我
在 Windows 窗体应用程序中,我们的数据 GridView 有很多事件,如行鼠标双击或行单击以及额外的...... 但是在 WPF 中我找不到这些事件。我如何将行鼠标双击添加到其中包含数据网格的
在这个项目中,代码 正确编译和执行 ;但是,我需要帮助解决两个问题: VS2012 WPF 设计器不适用于此 XAML 文件。它显示消息设计 View 对于 x64 和 ARM 目标平台不可用。 我收
目前我正在设计 WPF ScrollViewer,我发现了这个 Content="M 0 0 L 4 4 L 0 8 Z" 阅读 MSDN examples .现在我真的很想知道这意味着什么,但我无法
在 WPF 中,元素的可见性可以为“可见”,但实际上在屏幕上不可见,因为它的父元素(或父元素的父元素)具有折叠的可见性。 我希望能够知道一个元素是否实际呈现在屏幕上,而不必遍历可视化树检查父元素。 有
我应该使用 ApplicationCommands.Close用于关闭模式对话框还是该命令被认为是为关闭应用程序保留的?如果是后者,请大家创建Close每个命令 Dialog盒子或只是一个 Close
WPF 是否有任何可用的 piemenu 控件? 最佳答案 我在我的最爱中找到了这个,你可以看看: This 祝你今天过得愉快。 关于wpf - WPF 的菜单,我们在Stack Overflow上找
我正在尝试使用 WrapPanel 和两个 TextBlock 将星号 (*) 附加到某些文本的左侧,允许文本换行,并强制文本右对齐。通过创建一个 FlowDirection 设置为 RightToL
这里是场景(简化):我在Window上有一个控件(比如说一个Rectangle)。我迷上了MouseMove事件,以使其启动拖放操作。然后在MouseDown事件中进行动画处理,向右移动50个像素。但
我有一个 ListView ,它的项目来源是一个列表。我希望用户只选择一项。当我将 listview 的 SelectionMode 设置为 single 时,用户仍然可以选择多个项目,并且似乎 li
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
INotifyPropertyChanged 的目的是什么。我知道每当更改属性时都会触发此事件,但是 View /用户界面如何知道触发了此事件: 这是实现 INotifyPropertyChang
我正在查看工具箱中的 WPF 组件,但找不到 2005/2008 中存在的错误提供程序。 被移除了吗? 最佳答案 ErrorProvider是一个 Winforms 控件。 WPF 中没有等效项。但是
我试图在单击和双击 wpf Image 控件时有不同的行为。不幸的是,单击首先被触发,因此双击被忽略。 最佳答案 如果您改用 MouseDown 事件,则它在 EventArgs 中为 ClickCo
这可能吗? 我使用了一个框架控件并且:显示(例如:showwindow.xaml) 但是我得到这个错误: root element is not valid for navigation 最佳答案 确
我在蓝色背景的窗口上放置了一个扩展器,我想让扩展器的按钮与默认颜色不同(蓝色,它是从窗口接收的)。当我修改扩展器的背景属性时,它会将整个扩展器、标题和全部更改为新颜色。但是,我只想更改按钮本身。谁能指
我是一名优秀的程序员,十分优秀!