我有以下数据模板(以及相应的视图模型,未显示):
<DataTemplate DataType="{x:Type logic:SnapshotListViewModel}">
<ListBox ItemsSource="{Binding Snapshots}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
ItemsSource绑定到viewmodel中的Snapshot列表.我的目标是清除SelectedItem,因此列表框将返回其初始的未选定状态.视图模型实现IPropertyNotified.
我在XAML中添加了一个绑定,如下所示:
<ListBox SelectedItem={Binding SelectedSnapshot} .... />
Run Code Online (Sandbox Code Playgroud)
在视图模型中,我设置SelectedSnapshot = null,但没有任何反应,即使在属性上调用了RaisePropertyChanged.
我再次尝试使用SelectedIndex而不是SelectedItem.仍然没有运气.
我终于找到了解决方案,我将在下面详述.
我花了最近几天来追踪这个bug.我的绑定被分离了,我不明白为什么.我希望我的一个文本框在我的应用程序启动时具有焦点.所以我使用了一个附加属性来设置聚焦元素.我的一些数据绑定已停止工作.
出于某种原因,只需重新安排我的XAML就会导致bug消失.
举个例子:
<StackPanel>
<TextBox Text="{Binding Tb1}"/>
<TextBox Text="{Binding Tb2}"/>
<TextBox Text="{Binding Tb3}"
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
<TextBox Text="{Binding Tb4}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
Tb1,Tb2和Tb3的结合都起作用. Tb4绑定被破坏了. 如果我交换最后两行,像这样:
<StackPanel>
<TextBox Text="{Binding Tb1}"/>
<TextBox Text="{Binding Tb2}"/>
<TextBox Text="{Binding Tb4}"/>
<TextBox Text="{Binding Tb3}"
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
然后所有的绑定工作得很好.
出于某种原因,在XAML中设置focus元素会导致其后的所有绑定中断.
有没有人见过这个?如果是这样,你知道为什么会这样吗?
我想使用JSON.Net反序列化此架构.
{
"color" : {
"type" : "String",
"description" : "What color do you want your taco",
"required" : false,
"default" : "Green",
"options" : [ "Green", "Blue", "Red"]
},
"include_beans" : {
"type" : "Boolean",
"description" : "Do you want beans on your taco",
"required" : false,
"default" : false
},
"pounds" : {
"type" : "Double",
"description" : "How many pounds of meat do you want?",
"required" : false,
"default" : 0.1
},
"count" : {
"type" : …
Run Code Online (Sandbox Code Playgroud) 当我从 VS 2015 调试器运行我的程序时,我单击程序主窗口中的一个按钮,打开另一个窗口。当我关闭该窗口时,程序焦点返回到 MainWindow.xaml。然后调试器会在预览选项卡中自动打开 MainWindow.xaml 的代码文件。这很烦人。我几乎从不需要查看这个文件,但它总是在我的调试器中打开。即使我关闭预览模式,文件也会在普通选项卡中打开。我在测试我的程序时无法打开这个文件。
这是一个错误吗?这是我可以关闭的功能吗?
在我的程序中,我创建了一个长目录分支树.当我完成了这棵树的叶子上的某些文件时,我删除了它们,但最终我得到了很多空的父目录.我也想删除它们.但是,我不能只递归删除所有这些父目录,因为它们中的一些还有我无法删除的子项.
示例:C:\ MyProject\Project1\file1\file2\file3\file4\file5\document.txt
如果我删除document.txt,我还想删除路径中的所有其他空文件夹.但是,文件3中有一些东西(除了file4),所以我不能删除它或它上面的任何东西.所以在这种情况下,将删除file4和file5.
将根目录视为Project1.我不想删除上面的任何内容.
有没有人写过这样的东西?
基本上,我可以调用的东西,我可以指定我试图从树中删除的路径(下面的第一个arg),以及树的根(第二个arg).
DeleteEmptySubDirectoriesInPath("C:\MyProject\Project1\file1\file2\file3\file4\file5\",
"C:\MyProject\Project1");
Run Code Online (Sandbox Code Playgroud)
查看它的另一种方法是Directory.CreateDirectory的反转.这是我用来生成这些长分支的函数.现在我需要在完成时删除它们而不会打扰其他任何东西.
c# ×3
wpf ×2
xaml ×2
data-binding ×1
json ×1
json.net ×1
jsonschema ×1
mvvm ×1
selecteditem ×1