如何在WPF中绑定到此场景中的对象方法?
public class RootObject
{
public string Name { get; }
public ObservableCollection<ChildObject> GetChildren() {...}
}
public class ChildObject
{
public string Name { get; }
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<TreeView ItemsSource="some list of RootObjects">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type data:RootObject}"
ItemsSource="???">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type data:ChildObject}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
Run Code Online (Sandbox Code Playgroud)
在这里,我想绑定到GetChildren每个RootObject树上的方法.
编辑绑定到一个ObjectDataProvider似乎不起作用,因为我绑定到一个项目列表,并且ObjectDataProvider需要一个静态方法,或者它创建它自己的实例并使用它.
例如,使用Matt的答案,我得到:
System.Windows.Data错误:33:ObjectDataProvider无法创建对象; 类型= 'RootObject'; Error ='构造函数的参数错误.'
System.Windows.Data错误:34:ObjectDataProvider:尝试在类型上调用方法失败; 方法= '的GetChildren'; 类型= 'RootObject'; Error ='无法在目标上调用指定的成员.TargetException:'System.Reflection.TargetException:非静态方法需要一个目标.
无法绑定到'ngModel',因为它不是'input'元素的已知属性,并且没有匹配的指令和相应的属性
注意:即时通讯使用alpha.31
import { Component, View, bootstrap } from 'angular2/angular2'
@Component({
selector: 'data-bind'
})
@View({
template:`
<input id="name" type="text"
[ng-model]="name"
(ng-model)="name = $event" />
{{ name }}
`
})
class DataBinding {
name: string;
constructor(){
this.name = 'Jose';
}
}
bootstrap(DataBinding);
Run Code Online (Sandbox Code Playgroud) 在AngularJS中,如何在没有双向数据绑定的情况下呈现值?出于性能原因,或者甚至在给定时间点渲染值,可能希望这样做.
以下示例都使用数据绑定:
<div>{{value}}</div>
<div data-ng-bind="value"></div>
如何在value 没有任何数据绑定的情况下渲染?
有谁知道如何在WPF(3.5SP1)中数据绑定WebBrowser的.Source属性?我有一个listview,我希望在左边有一个小的WebBrowser,在右边有内容,并在每个WebBrowser的源中为每个绑定到列表项的对象中的URI进行数据绑定.
这是我迄今为止的概念证明,但" <WebBrowser Source="{Binding Path=WebAddress}""不编译.
<DataTemplate x:Key="dealerLocatorLayout" DataType="DealerLocatorAddress">
<StackPanel Orientation="Horizontal">
<!--Web Control Here-->
<WebBrowser Source="{Binding Path=WebAddress}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Width="300"
Height="200"
/>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=CompanyName}" FontWeight="Bold" Foreground="Blue" />
<TextBox Text="{Binding Path=DisplayName}" FontWeight="Bold" />
</StackPanel>
<TextBox Text="{Binding Path=Street[0]}" />
<TextBox Text="{Binding Path=Street[1]}" />
<TextBox Text="{Binding Path=PhoneNumber}"/>
<TextBox Text="{Binding Path=FaxNumber}"/>
<TextBox Text="{Binding Path=Email}"/>
<TextBox Text="{Binding Path=WebAddress}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud) 有人可以描述两者之间的区别对于我的项目.
目前我有一个List<MyClass>并将BindingSource设置为它,并将DataGridView设置为BindingSource.
我已经实现了IEditableObject,当调用CancelEdit时,我将我的对象恢复到它的状态Memberwise.Clone()
将我的List更改为BindingList会解决这个问题,使用BindingList有什么好处?
我需要在代码中设置绑定.
我似乎无法做到这一点.
这是我尝试过的:
XAML:
<TextBox Name="txtText"></TextBox>
Run Code Online (Sandbox Code Playgroud)
代码背后:
Binding myBinding = new Binding("SomeString");
myBinding.Source = ViewModel.SomeString;
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(txtText, TextBox.TextProperty, myBinding);
Run Code Online (Sandbox Code Playgroud)
视图模型:
public string SomeString
{
get
{
return someString;
}
set
{
someString= value;
OnPropertyChanged("SomeString");
}
}
Run Code Online (Sandbox Code Playgroud)
我设置它时,属性不会更新.
我究竟做错了什么?
我正在编写一个WPF程序,我试图通过一些可重复的方法(如样式或模板)找出一种在TextBox中格式化数据的方法.我有很多TextBoxes(确切地说是95),每一个都绑定到自己的数值数据,每个数据都可以定义自己的分辨率.例如,如果数据为99.123,分辨率为2,则应显示99.12.同样,数据值99和分辨率3应显示为99.000(而不是99).有没有办法做到这一点?
编辑: 我应该澄清一下,我正在处理的当前屏幕上有95个TextBox,但我希望程序中各个屏幕的每个TextBox都能显示正确的小数位数.现在我考虑一下,其中一些是TextBoxes(就像我现在正在处理的屏幕),有些是DataGrids或ListViews,但是如果我能弄清楚如何使它适用于TextBoxes我敢肯定我可以想象它也用于其他控件.
在这种情况下,没有太多的代码可以共享,但我会尝试使其更清晰:
我有一个View Model,它包含以下属性(vb.net):
Public ReadOnly Property Resolution As Integer
Get
Return _signal.DisplayResolution
End Get
End Property
Public ReadOnly Property Value As Single
Get
Return Math.Round(_signal.DisplayValue, Resolution)
End Get
End Property
Run Code Online (Sandbox Code Playgroud)
在XAML中我有:
<UserControl.Resources>
<vm:SignalViewModel x:Key="Signal" SignalPath="SomeSignal"/>
</UserControl.Resources>
<TextBox Grid.Column="3" IsEnabled="False" Text="{Binding Path=Value, Source={StaticResource Signal}, Mode=OneWay}" />
Run Code Online (Sandbox Code Playgroud)
EDIT2(我的解决方案): 事实证明,在离开电脑一段时间之后,我回来找到一个简单的答案,盯着我的脸.格式化视图模型中的数据!
Public ReadOnly Property Value As String
Get
Return (Strings.FormatNumber(Math.Round(_signal.DisplayValue, _signal.DisplayResolution), _signal.DisplayResolution))
End Get
End Property
Run Code Online (Sandbox Code Playgroud) 我遇到了WPF和命令的问题,这些问题绑定到ItemsControl的DataTemplate中的Button.这种情况很简单.ItemsControl绑定到一个对象列表,我希望能够通过单击一个Button删除列表中的每个对象.Button执行命令,Command负责删除.CommandParameter绑定到我要删除的Object.这样我知道用户点击了什么.用户应该只能删除他们的"自己的"对象 - 所以我需要在Command的"CanExecute"调用中进行一些检查,以验证用户是否具有正确的权限.
问题是传递给CanExecute的参数在第一次被调用时是NULL - 所以我无法运行逻辑来启用/禁用命令.但是,如果我启用了allways,然后单击按钮执行命令,则会正确传入CommandParameter.这意味着对CommandParameter的绑定正在起作用.
ItemsControl和DataTemplate的XAML如下所示:
<ItemsControl
x:Name="commentsList"
ItemsSource="{Binding Path=SharedDataItemPM.Comments}"
Width="Auto" Height="Auto">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button
Content="Delete"
FontSize="10"
Command="{Binding Path=DataContext.DeleteCommentCommand, ElementName=commentsList}"
CommandParameter="{Binding}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
所以你可以看到我有一个评论对象列表.我希望将DeleteCommentCommand的CommandParameter绑定到Command对象.
所以我想我的问题是:以前有没有人遇到过这个问题?我的命令会调用CanExecute,但第一次参数总是为NULL - 为什么会这样?
更新:我能够将问题缩小一点.我添加了一个空的Debug ValueConverter,以便在CommandParameter是数据绑定时输出消息.事实证明,在CommandParameter绑定到按钮之前执行CanExecute方法.我试图在Command之前设置CommandParameter(如建议的那样) - 但它仍然不起作用.有关如何控制它的任何提示.
Update2:有没有办法检测绑定何时"完成",以便我可以强制重新评估命令?另外 - 我有一个问题,我有多个按钮(ItemsControl中的每个项目一个)绑定到Command对象的同一个实例?
Update3:我已经将错误的副本上传到我的SkyDrive:http://cid-1a08c11c407c0d8e.skydrive.live.com/self.aspx/Code%20samples/CommandParameterBinding.zip
{Binding Path=.}WPF绑定意味着什么?
我看到有些人使用它,但找不到任何解释.
绑定语法中是否还有其他特殊符号(除了{Binding /})?
我有一个WPF Window,在某个地方有一个ListView我绑定List<string>到的地方.
现在在什么地方我ListView有一个TextBox和Content属性设置为{Binding}.
但这是速记.如何编写完整绑定以绑定到自身?
{Binding Path=Self}不起作用,也不起作用{Binding Self}(后者是前者的捷径).
data-binding ×10
wpf ×7
xaml ×6
c# ×3
.net ×2
.net-3.5 ×1
angular ×1
angularjs ×1
binding ×1
browser ×1
command ×1
datagridview ×1
javascript ×1
self ×1
typescript ×1
winforms ×1