出于好奇,为什么下面的程序
1 = 0
"hello" = "world"
Run Code Online (Sandbox Code Playgroud)
由 GHC 有效和编译?这仅仅是一个错误还是一个功能?谢谢!
我有一个WCF服务,通过如下结构传递状态更新:
[DataContract]
public struct StatusInfo
{
[DataMember] public int Total;
[DataMember] public string Authority;
}
...
public StatusInfo GetStatus() { ... }
Run Code Online (Sandbox Code Playgroud)
我在ViewModel中公开了一个属性,如下所示:
public class ServiceViewModel : ViewModel
{
public StatusInfo CurrentStatus
{
get{ return _currentStatus; }
set
{
_currentStatus = value;
OnPropertyChanged( () => CurrentStatus );
}
}
}
Run Code Online (Sandbox Code Playgroud)
和XAML一样:
<TextBox Text="{Binding CurrentStatus.Total}" />
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我在输出窗口中看到错误,指示无法找到Total属性.我检查并仔细检查,我输入正确.我突然意识到错误明确表明找不到"财产".因此,向结构添加属性使其工作得很好.但这对我来说似乎很奇怪,WPF无法处理对字段的单向绑定.从语法上讲,您在代码中访问它们是相同的,并且仅为StatusInfo结构创建自定义视图模型似乎很愚蠢.我错过了WPF绑定的一些内容吗?你能绑定到一个字段还是属性绑定唯一的方法?
我有一组表示菜单项的对象(viewmodels).当单击MenuItem时,每个命令都要执行.
如果我想静态地进行菜单,我这样做:
<ContextMenu>
<MenuItem Header="{Binding Text1}" Command={Binding Command1}>
<MenuItem Header="{Binding Text2}" Command={Binding Command2}>
</ContextMenu>
Run Code Online (Sandbox Code Playgroud)
但是当我事先不知道这些项目(它们来自集合)时,我需要分配ContextMenu.ItemsSource - 并将文本放入ItemTemplate.
<ContextMenu ItemsSource="{Binding MyMenuItems}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text2}" /> <!-- But where to put Command binding? TextBlock.Command makes no sense, and we have no access to MenuItem! -->
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
Run Code Online (Sandbox Code Playgroud)
但是,这样,我无法将Command绑定到 - 因为我无法获取每一行的MenuItem!
请问有什么建议吗?感谢你们!
在Silverlight 4中会有RelativeSource FindAncestor,AncestorType ......吗?
我已经看到在同一个项目中使用了这两种样式,我想知道它们之间是否有任何语义差异,或者是否会推荐其他样式以及为什么.
这是我的绑定(缩短,Command-Property也绑定)
<MenuItem Header="Key" CommandParameter="{Binding StringFormat='Key: {0}', Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
Run Code Online (Sandbox Code Playgroud)
ContectMenu的PlacementTarget的Tag-Property是一个类似String的属性
"Short.Plural"
Run Code Online (Sandbox Code Playgroud)
我期望在Command-Handler中收到的是:
Key: Short.Plural
Run Code Online (Sandbox Code Playgroud)
但我真正得到的是:
Short.Plural
Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法将方法的返回值绑定到JSF组件中.我会更好地解释自己.假设我有这样一个类:
public class Document {
private List<Attachment> attachments;
//getter and setter here
}
Run Code Online (Sandbox Code Playgroud)
这个类通过一个名为currentDocument的属性中的注册托管bean暴露给jsf,并以这种方式用于jsf
<h:outputText value="#{myManagedBean.currentDocument.attachment.size}" />
Run Code Online (Sandbox Code Playgroud)
我知道,这是不正确的.但是这样做的正确方法是什么?我应该在Document类上创建一个属性,让我们说numberOfAttachments,并绑定到那个,或者有一种方法直接绑定方法的返回值?
我试图将一个非常简单的属性绑定到TextBlock,但我必须在代码隐藏(C#)中完成所有操作.
我想做的是:
public string SomeText { get; set; }
Run Code Online (Sandbox Code Playgroud)
在我尝试TextBlock上的绑定之后:
Binding myBinding = new Binding(SomeText);
myTextBlock.SetBinding(TextBlock.TextProperty, myBinding);
Run Code Online (Sandbox Code Playgroud)
如何使TextBlock的Text属性与Property保持一致SomeText.
我没有找到这两个问题的简单答案:
在删除属性实例之前是否必须删除侦听器(在其他任何地方都不使用侦听器)?
BooleanProperty bool = new SimpleBooleanProperty();
bool.addListener(myListener);
bool.removeListener(myListener); // is it necessary to do this?
bool = null;
Run Code Online (Sandbox Code Playgroud)在删除属性实例之前,我是否必须取消绑定单向有界属性?
BooleanProperty bool = new SimpleBooleanProperty();
bool.bind(otherBool);
bool.unbind(); // is it necessary to do this?
bool = null;
Run Code Online (Sandbox Code Playgroud)我正在尝试使用绑定创建动态菜单.我的viewmodel我有一个包含标题和命令的对象列表.但是,它不起作用.我认为问题出在数据模板中.请参阅下面的代码:
<Menu Background="{x:Null}" Grid.Row="0" Grid.Column="1" Panel.ZIndex="2" Width="865" Height="85" HorizontalAlignment="Left" ItemsSource="{Binding Path=MenuItems}">
<Menu.ItemTemplate>
<HierarchicalDataTemplate DataType="MenuItemViewModel" ItemsSource="{Binding Path=MenuItems}">
<MenuItem Header="{Binding Header}" Style="{DynamicResource MenuItemStyle1}" ItemsSource="{Binding Path=MenuItems}" Padding="10,12,10,0" Height="44.1" Margin="30,0,0,0" FontWeight="Bold">
<MenuItem.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</MenuItem.ItemsPanel>
</MenuItem>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Header}" Style="{DynamicResource MenuItemStyle1}" Padding="0,8,0,0" Height="38">
</MenuItem>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</Menu.ItemTemplate>
</Menu>
Run Code Online (Sandbox Code Playgroud)
结果仅显示第一个菜单.子菜单没有显示,但它们在那里,因为菜单有子项,箭头在菜单标题后打印.
任何人都可以在绑定上发现错误吗?还是有什么建议吗?
仅供参考,MenuItems是一个MenuItemViewModel对象列表,它有一个标题和一个名为MenuItems的MenuItemViewModel对象(子菜单)列表.
binding ×10
wpf ×6
.net ×2
c# ×1
code-behind ×1
command ×1
contextmenu ×1
el ×1
field ×1
ghc ×1
haskell ×1
javafx ×1
jsf ×1
jsf-2 ×1
listener ×1
literals ×1
memory-leaks ×1
menuitem ×1
silverlight ×1
xaml ×1