我在将单选按钮绑定到属性具有布尔值的对象时遇到问题.我正在尝试显示从$资源中检索到的考试题目.
HTML:
<label data-ng-repeat="choice in question.choices">
<input type="radio" name="response" data-ng-model="choice.isUserAnswer" value="true" />
{{choice.text}}
</label>
Run Code Online (Sandbox Code Playgroud)
JS:
$scope.question = {
questionText: "This is a test question.",
choices: [{
id: 1,
text: "Choice 1",
isUserAnswer: false
}, {
id: 2,
text: "Choice 2",
isUserAnswer: true
}, {
id: 3,
text: "Choice 3",
isUserAnswer: false
}]
};
Run Code Online (Sandbox Code Playgroud)
使用此示例对象,"isUserAnswer:true"属性不会导致选择单选按钮.如果我将布尔值封装在引号中,它就可以工作.
JS:
$scope.question = {
questionText: "This is a test question.",
choices: [{
id: 1,
text: "Choice 1",
isUserAnswer: "false"
}, {
id: 2,
text: "Choice 2", …Run Code Online (Sandbox Code Playgroud) 我目前正在使用TextBlock以下命令绑定名为的属性的值Name:
<TextBlock Text="{Binding Name}" />
Run Code Online (Sandbox Code Playgroud)
现在,我想绑定另一个名为ID相同的属性TextBlock.
是否可以将两个或多个值绑定到同一个值TextBlock?是否可以通过简单的连接来完成Name + ID,如果没有,可以如何处理它?
我有一个似乎没有更新SelectedItem/SelectedValue的ComboBox.
组合框ItemsSource绑定到上列出一串RAS电话簿条目作为一个的CollectionView的视图模型类的属性.然后,我必然(在不同的时间)两个SelectedItem或SelectedValue到视图模型的另一个属性.我在save命令中添加了一个MessageBox来调试数据绑定设置的值,但是没有设置SelectedItem/ SelectedValuebinding.
ViewModel类看起来像这样:
public ConnectionViewModel
{
private readonly CollectionView _phonebookEntries;
private string _phonebookeEntry;
public CollectionView PhonebookEntries
{
get { return _phonebookEntries; }
}
public string PhonebookEntry
{
get { return _phonebookEntry; }
set
{
if (_phonebookEntry == value) return;
_phonebookEntry = value;
OnPropertyChanged("PhonebookEntry");
}
}
}
Run Code Online (Sandbox Code Playgroud)
_phonebookEntries集合正在构造函数中从业务对象初始化.ComboBox XAML看起来像这样:
<ComboBox ItemsSource="{Binding Path=PhonebookEntries}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValue="{Binding Path=PhonebookEntry}" />
Run Code Online (Sandbox Code Playgroud)
我只在下拉列表中显示的实际字符串值感兴趣的对象,因为这不是任何其他属性是我需要跨到RAS通过时,我想使VPN连接,因此价值DisplayMemberPath和SelectedValuePath都的名称属性ConnectionViewModel.ComboBox DataTemplate应用于ItemsControl一个Window,其DataContext已设置为ViewModel实例.
ComboBox正确显示项目列表,我可以在UI中选择一个没有问题.但是,当我从命令中显示消息框时,PhonebookEntry属性仍然具有初始值,而不是ComboBox中的选定值.其他TextBox实例正在更新并在MessageBox中显示.
使用数据绑定ComboBox我错过了什么?我做了很多搜索,似乎找不到任何我做错的事.
这是我所看到的行为,但是在我的特定情况下,它不能用于某种原因.
我有一个MainWindowViewModel,它有一个CollectionViewConnectionViewModel.在MainWindowView.xaml文件代码隐藏中,我将DataContext设置为MainWindowViewModel.MainWindowView.xaml ItemsControl …
我试图找到一个简单的例子,其中枚举按原样显示.我见过的所有示例都尝试添加漂亮的显示字符串,但我不希望这种复杂性.
基本上我有一个类,它包含我绑定的所有属性,首先将DataContext设置为此类,然后在xaml文件中指定这样的绑定:
<ComboBox ItemsSource="{Binding Path=EffectStyle}"/>
Run Code Online (Sandbox Code Playgroud)
但是这并没有在ComboBoxas项中显示枚举值.
我很难将简单的静态字符串属性绑定到文本框.
这是具有静态属性的类:
public class VersionManager
{
private static string filterString;
public static string FilterString
{
get { return filterString; }
set { filterString = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
在我的xaml中,我只想将此静态属性绑定到文本框:
<TextBox>
<TextBox.Text>
<Binding Source="{x:Static local:VersionManager.FilterString}"/>
</TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
所有内容都编译,但在运行时,我得到以下异常:
无法将属性"Source"中的值转换为"System.Windows.Markup.StaticExtension"类型的对象.标记文件'BurnDisk中的对象'System.Windows.Data.Binding'出错;组件/ selectversionpagefunction.xaml'第57行位置29.
知道我做错了什么吗?
我是Angular2的新手,我有一点问题:
在我的Login-Component-HTML中,我有两个复选框,我希望以双向数据绑定方式绑定到Login-Component-TypeScript.
这是HTML:
<div class="checkbox">
<label>
<input #saveUsername [(ngModel)]="saveUsername.selected" type="checkbox" data-toggle="toggle">Save username
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
这是Component.ts:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Variables } from '../../services/variables';
@Component({
selector: 'login',
moduleId: module.id,
templateUrl: 'login.component.html',
styleUrls: ['login.component.css']
})
export class LoginComponent implements OnInit {
private saveUsername: boolean = true;
private autoLogin: boolean = true;
constructor(private router: Router, private variables: Variables) { }
ngOnInit() {
this.loginValid = false;
// Get user name from local storage …Run Code Online (Sandbox Code Playgroud) 我正在寻找如何绑定到AngularJS中的服务属性的最佳实践.
我已经通过多个示例来了解如何绑定到使用AngularJS创建的服务中的属性.
下面我有两个如何绑定到服务中的属性的示例; 他们都工作.第一个示例使用基本绑定,第二个示例使用$ scope.$ watch绑定到服务属性
在绑定到服务中的属性时,这些示例中的任何一个都是首选的,还是有其他我不知道的选项会被推荐?
这些示例的前提是服务应每5秒更新其属性"lastUpdated"和"calls".更新服务属性后,视图应反映这些更改.这两个例子都成功地运作了 我想知道是否有更好的方法.
基本绑定
可以在此处查看和运行以下代码:http://plnkr.co/edit/d3c16z
<html>
<body ng-app="ServiceNotification" >
<div ng-controller="TimerCtrl1" style="border-style:dotted">
TimerCtrl1 <br/>
Last Updated: {{timerData.lastUpdated}}<br/>
Last Updated: {{timerData.calls}}<br/>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module("ServiceNotification", []);
function TimerCtrl1($scope, Timer) {
$scope.timerData = Timer.data;
};
app.factory("Timer", function ($timeout) {
var data = { lastUpdated: new Date(), calls: 0 };
var updateTimer = function () {
data.lastUpdated = new Date();
data.calls += 1;
console.log("updateTimer: " + data.lastUpdated);
$timeout(updateTimer, 5000); …Run Code Online (Sandbox Code Playgroud) data-binding angularjs angularjs-service angularjs-controller
我已经阅读了很多,Backbone没有做双向绑定,但我并不完全理解这个概念.
有人能给我一个例子,说明双向绑定如何在MVC代码库中运行,以及它如何与Backbone无关?
我在Stack Overflow上搜索了一下,没有找到任何好的答案.
我所相信的是,数据绑定是一个通用术语,有不同的实现方式,如观察者模式或发布/子模式.使用Observer模式,Observable更新其观察者.使用Pub/Sub,0-many发布者可以发布某些类的消息,0-many订阅者可以订阅某些类的消息.
是否有其他实施"数据绑定"的模式?
data-binding model-view-controller design-patterns publish-subscribe observer-pattern
我正在寻找在Spring MVC中绑定和转换数据的最简单和最简单的方法.如果可能,不进行任何xml配置.
到目前为止,我一直在使用PropertyEditors:
public class CategoryEditor extends PropertyEditorSupport {
// Converts a String to a Category (when submitting form)
@Override
public void setAsText(String text) {
Category c = new Category(text);
this.setValue(c);
}
// Converts a Category to a String (when displaying form)
@Override
public String getAsText() {
Category c = (Category) this.getValue();
return c.getName();
}
}
Run Code Online (Sandbox Code Playgroud)
和
...
public class MyController {
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Category.class, new CategoryEditor());
}
...
}
Run Code Online (Sandbox Code Playgroud)
它很简单:两个转换都在同一个类中定义,绑定很简单.如果我想在我的所有控制器上进行通用绑定,我仍然可以在我的xml配置中添加3行 …
data-binding ×10
wpf ×4
angularjs ×2
c# ×2
xaml ×2
.net ×1
angular ×1
backbone.js ×1
binding ×1
checkbox ×1
combobox ×1
html ×1
java ×1
javascript ×1
model ×1
multibinding ×1
mvvm ×1
radio ×1
spring ×1
spring-mvc ×1
textblock ×1
typescript ×1