如何将视图模型属性绑定到ListBox.SelectedItem属性?
我已经创建了一个简单的MVVM演示来尝试解决这个问题.我的视图模型具有以下属性:
private ObservableCollection<DisneyCharacter> p_DisneyCharacters;
public ObservableCollection<DisneyCharacter> DisneyCharacters
{
get { return p_DisneyCharacters; }
set
{
p_DisneyCharacters = value;
base.FirePropertyChangedEvent("DisneyCharacters");
}
}
private DisneyCharacter p_SelectedItem;
public DisneyCharacter SelectedItem
{
get { return p_SelectedItem; }
set
{
p_SelectedItem = value;
base.FirePropertyChangedEvent("SelectedItem");
}
}
Run Code Online (Sandbox Code Playgroud)
我想将SelectedItem属性绑定到列表框中选定的项目.这是列表框的XAML:
<ListBox ItemTemplate="{StaticResource MasterTemplate}"
ItemsSource="{Binding Path=DisneyCharacters}"
SelectedItem="{Binding Path=Selectedtem, Mode=TwoWay}"
HorizontalAlignment="Stretch" />
Run Code Online (Sandbox Code Playgroud)
这是我的问题:当我更改列表框中的选择时,视图模型SelectedItem属性未更新.
我做了一个测试,我暂时用SelectedIndex属性替换了视图模型SelectedItem属性,并将其绑定到ListBox.SelectedIndex属性.该属性更新很好 - 它只是我无法工作的SelectedItem属性.
那么,我该如何修复SelectedItem绑定?谢谢你的帮助.
我有一个字典对象,<string, string>并希望将其绑定到转发器.但是,我不确定在aspx标记中放置什么来实际显示键值对.没有抛出错误,我可以使用它List.如何在转发器中显示字典?
我已经看过很多WPF Binding示例并且已经在学习MVVM的许多不同地方使用了这个功能,但是对我来说似乎很不一致的是当你在绑定字符串中指定"Path ="时只需简单地说输入要绑定的属性.例如,以下XAML属性之间的功能区别是什么:
DataMemberBinding="{Binding SomeProperty}"
DataMemberBinding="{Binding Path=SomeProperty}"
Run Code Online (Sandbox Code Playgroud) 我正在使用带有对象数据绑定的DataGridView来显示有关系统中日志记录实体的信息,这些信息是通过SOAP从远程服务检索的.其中一列称为"上次操作",表示实体最后一次记录消息.这是一个System.DateTime价值.当我读取SOAP响应(下面的示例)时,他的时间戳显然包含所有信息,最多包含第二个分数.
<LoggingEntity>
<host>marty86ce</host>
<process>10148</process>
<logger>http_core</logger>
<appName>httpd</appName>
<ffda>true</ffda>
<lastAction>2010-10-27T12:00:19.5117509Z</lastAction>
<lastHeartbeat>0001-01-01T00:00:00</lastHeartbeat>
<channelId>em_9BA2A2B4D0B6E66</channelId>
<ffdaChannelId>em_E7C8D1D4DE8EEB9</ffdaChannelId>
</LoggingEntity>
Run Code Online (Sandbox Code Playgroud)
当我在桌子上显示它时,我可以读取分钟
当我按下刷新按钮时,我使用以下代码进行数据绑定
public void RefreshEntities()
{
IEntityManagement management = EntityPlugin.GetProxy();
LoggingEntity[] result = management.FindLoggingEntities(new TemplateQuery { ffdaSpecified = true, ffda = true }); //Remote invocation
Invoke(new MethodInvoker(delegate { gridEntities.DataSource = result; })); //THIS does the data binding from the array
Invoke(new MethodInvoker(delegate { btnRefresh.Enabled = true; }));
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何控制数据绑定值的列格式.我认为格式dd/MM/yyyy hh:mm来自我的系统设置.如何以编程方式覆盖格式设置?
先感谢您
用于Logbus-ng开源项目的Subversion(FFDAGui程序)的完整解决方案代码.
我正在开发一个RESTful接口,用于为JavaScript应用程序提供JSON数据.
在服务器端,我使用Grails 1.3.7并使用GORM域对象进行持久化.我实现了一个自定义JSON Marshaller来支持编组嵌套域对象
以下是示例域对象:
class SampleDomain {
static mapping = { nest2 cascade: 'all' }
String someString
SampleDomainNested nest2
}
Run Code Online (Sandbox Code Playgroud)
和
class SampleDomainNested {
String someField
}
Run Code Online (Sandbox Code Playgroud)
SampleDomain资源在URL/rs/sample/so/rs/sample/1下发布,指向ID为1的SampleDomain对象
当我使用自定义json marshaller(/ rs/sample/1上的GET)渲染资源时,我得到以下数据:
{
"someString" : "somevalue1",
"nest2" : {
"someField" : "someothervalue"
}
}
Run Code Online (Sandbox Code Playgroud)
这正是我想要的.
现在出现了问题:我尝试通过PUT将相同的数据发送到资源/ rs/sample/1.
要将json数据绑定到域对象,处理请求的控制器调用def domain = SampleDomain.get(id)以及domain.properties = data数据是unmarshalled对象的位置.
"someString"字段的绑定工作得很好,但是嵌套对象没有使用嵌套数据填充,因此我得到一个错误,即属性"nest2"为null,这是不允许的.
我已经尝试过实现一个自定义PropertyEditorSupport以及一个StructuredPropertyEditor并为该类注册编辑器.
奇怪的是,当我提供非嵌套值时,编辑器才会被调用.所以当我通过PUT将以下内容发送到服务器时(这没有任何意义;))
{
"someString" : "somevalue1",
"nest2" : "test"
}
Run Code Online (Sandbox Code Playgroud)
至少会调用属性编辑器.
我查看了代码GrailsDataBinder.我发现通过指定关联的路径而不是提供地图,设置关联的属性似乎有效,因此以下工作原理:
{
"someString" : "somevalue1", …Run Code Online (Sandbox Code Playgroud) 我知道这是一个陈旧且经过100次回答的问题,但随着最新版本的发布变得越来越复杂,因此引起了很多困惑.我想知道在指令中为属性声明数据绑定的四种当前可用方法之间的区别.特别:
@ 文字绑定= 双向绑定& 方法绑定(虽然有些人称之为单向绑定)< 单向绑定我对最后两个之间的差异特别感兴趣,因为它们似乎具有重叠的功能,我实在无法分辨出一个与另一个的区别和优势.
我是Angular2的新手,在全局的web中,我想在检查checkbox和/或取消选中时使用Material-Design,在数据库中启动一个更改oject参数值的操作,我试过[(ngModel)]但没有发生任何事情.这个想法是,我必须添加一些命题与checked | unchecked状态,判断它是一个true或false命题.这是命题模型
export class PropositionModel {
id:string;
wordingP:string; // the proposition
propStatus:Boolean; // the proposition status
}
Run Code Online (Sandbox Code Playgroud)
这是一个命题的Html代码:
<div class="uk-width-xlarge-1-1 uk-width-medium-1-2">
<div (submit)="addProp1()" class="uk-input-group">
<span class="uk-input-group-addon"><input type="checkbox" data-md-icheck/></span>
<label>Proposition 1</label>
<input [(ngModel)]="proposition1.wordingP" type="text" class="md-input" required class="md-input"/>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是用于添加命题的TypeScript代码:
addProp1() {
this.proposition1 = new PropositionModel();
this.proposition1.propStatus = false;
this.propositionService.addProposition(this.proposition1)
.subscribe(response=> {
console.log(response);
console.log(this.proposition1);
this.proposition1 = new PropositionModel();})
}
Run Code Online (Sandbox Code Playgroud)
并且你可以看到我false默认为它做了一个proposition status,我想在检查命题后改变它.这是一个图像如何寻找更好的问题理解.

有什么帮助吗?
尝试为int创建一个@BindingConversion时遇到一个神秘的问题.
以下代码适用于浮点数到字符串:
XML:
...
<variable
name="myViewModel"
type="... .SomeModel" />
...
<TextView
style="@style/StyleStuff"
android:text="@{myViewModel.number}" />
Run Code Online (Sandbox Code Playgroud)
码:
public class SomeModel {
public ObservableFloat number = new ObservableFloat();
}
Run Code Online (Sandbox Code Playgroud)
和设置:
viewModel.number.set(3.14f);
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试对字符串进行同样的操作,我会崩溃.
public ObservableInt number = new ObservableInt();
Run Code Online (Sandbox Code Playgroud)
同
viewModel.number.set(42);
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:
FATAL EXCEPTION: main
Process: ...myapplication, PID: 14311
android.content.res.Resources$NotFoundException: String resource ID #0xfa0
at android.content.res.Resources.getText(Resources.java:1123)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:4816)
at ...executeBindings(ActivityAdaptersBinding.java:336)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:355)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!
我使用一个典型的样式来显示验证错误作为IErrorDataInfo的工具提示,如下所示,它可以正常工作.
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
但是当我尝试为这样的ComboBox做同样的事情时,它失败了
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
我在输出窗口中得到的错误是:
System.Windows.Data错误:17:无法从'(Validation.Errors)'获取'Item []'值(类型'ValidationError')(类型'ReadOnlyObservableCollection`1').BindingExpression:路径=(0)[0] .ErrorContent; DataItem ='ComboBox'(Name ='ownerComboBox'); target元素是'ComboBox'(Name ='ownerComboBox'); target属性是'ToolTip'(类型'Object')ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围.参数名称:index'
奇怪的是,如果我更改任何ComboBox值,它也会尝试在关闭窗口时进行无效的数据库更改(这也是发生绑定错误时)!
无法将值NULL插入列'EmpFirstName',表'OITaskManager.dbo.Employees'; 列不允许空值.INSERT失败.该语句已终止.
简单地通过评论风格完美的每一个作品.我该如何解决?
万一有人需要它,其中一个comboBox'xaml如下:
<ComboBox ItemsSource="{Binding Path=Employees}"
SelectedValuePath="EmpID"
SelectedValue="{Binding Path=SelectedIssue.Employee2.EmpID,
Mode=OneWay, ValidatesOnDataErrors=True}"
ItemTemplate="{StaticResource LastNameFirstComboBoxTemplate}"
Height="28" Name="ownerComboBox" Width="120" Margin="2"
SelectionChanged="ownerComboBox_SelectionChanged" />
<DataTemplate x:Key="LastNameFirstComboBoxTemplate">
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{1}, {0}" >
<Binding Path="EmpFirstName" />
<Binding Path="EmpLastName" />
</MultiBinding> …Run Code Online (Sandbox Code Playgroud) 我有一个庞大的项目,包括数据绑定,kotlin,匕首.我花了几天时间尝试使用几个stackoverflow的解决方案来构建它,并决定亲自询问它.
我假设一些第三方库使用数据绑定,因为添加此行没有帮助.
kapt 'com.android.databinding:compiler:3.0.0'
Run Code Online (Sandbox Code Playgroud)
Android Studio说:
'androidProcessor'依赖关系不会被识别为kapt注释处理器.请将配置名称更改为'kapt'以获取这些工件:'com.android.databinding:compiler:3.0.0'.
我尝试了什么,它没有帮助:
kapt {
generateStubs = true
}
Run Code Online (Sandbox Code Playgroud)
在local.properties中: kotlin.incremental=false
另一个SO解决方案没有帮助:
kapt ('com.android.databinding:compiler:3.0.0'){
force = true
}
Run Code Online (Sandbox Code Playgroud)
我的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'blockcanaryex'
apply plugin: 'kotlin-kapt'
apply plugin: 'newrelic'
apply plugin: 'kotlin-android-extensions'
def props = new Properties()
file("newrelic.properties").withInputStream { props.load(it) }
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "symbolic-ref", "--short", "HEAD"
standardOutput …Run Code Online (Sandbox Code Playgroud) data-binding ×10
c# ×3
wpf ×3
android ×2
angular ×1
angularjs ×1
asp.net ×1
checkbox ×1
datagridview ×1
datetime ×1
dictionary ×1
grails ×1
grails-orm ×1
html5 ×1
json ×1
kotlin ×1
mvvm ×1
repeater ×1
styles ×1
typescript ×1
validation ×1
winforms ×1
xaml ×1