我们有一个应用程序使用简单的单向绑定与GridView来显示一些数据.好吧,现在我们需要允许用户更改一些数据,所以我一直试图让双向数据绑定在GridView中工作.到目前为止,一切都正确显示,但在GridView中编辑单元格似乎什么都不做.我搞砸了什么?像这样的双向数据绑定甚至可能吗?我应该开始转换所有内容以使用不同的控件,比如DataGrid吗?
我写了一个小测试应用程序,显示我的问题.如果您尝试它,您将看到属性设置器在初始化后永远不会被调用.
XAML:
Title="Window1" Height="300" Width="300">
<Grid>
<ListView Name="TestList">
<ListView.View>
<GridView>
<GridViewColumn Header="Strings">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=String, Mode=TwoWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Bools">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Bool, Mode=TwoWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是相应的代码:
using System.Collections.Generic;
using System.Windows;
namespace GridViewTextbox
{
public partial class Window1 : Window
{
private List<TestRow> _rows = new List<TestRow>();
public Window1()
{
InitializeComponent();
_rows.Add(new TestRow("a", false));
_rows.Add(new TestRow("b", true));
_rows.Add(new TestRow("c", false));
TestList.ItemsSource = _rows;
TestList.DataContext = _rows;
}
} …
Run Code Online (Sandbox Code Playgroud) 根据Silverlight TwoWay绑定的工作原理,当我更改FirstName字段中的数据时,它应该更改CheckFirstName字段中的值.
为什么不是这样?
谢谢杰夫,就是这样,对于其他人:这里是可下载代码的完整解决方案.
XAML:
<StackPanel>
<Grid x:Name="GridCustomerDetails">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="300"/>
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="0" Grid.Column="0">First Name:</TextBlock>
<TextBox Margin="10" Grid.Row="0" Grid.Column="1" Text="{Binding FirstName, Mode=TwoWay}"/>
<TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="1" Grid.Column="0">Last Name:</TextBlock>
<TextBox Margin="10" Grid.Row="1" Grid.Column="1" Text="{Binding LastName}"/>
<TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="2" Grid.Column="0">Address:</TextBlock>
<TextBox Margin="10" Grid.Row="2" Grid.Column="1" Text="{Binding Address}"/>
</Grid>
<Border Background="Tan" Margin="10">
<TextBlock x:Name="CheckFirstName"/>
</Border>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
代码背后:
public Page()
{
InitializeComponent();
Customer customer = new Customer();
customer.FirstName …
Run Code Online (Sandbox Code Playgroud) 模型的setter和getter方法具有一个参数,如下所示:
public int getPrice(Object key) {
}
public void setPrice(Object key, int price) {
}
Run Code Online (Sandbox Code Playgroud)
XML看起来像这样:
<EditText
android:id="@+id/edtPrice"
style="@style/CreateShipperItemValueEditTextView"
android:layout_centerVertical="true"
android:hint="@string/hint_price"
android:inputType="number"
android:maxLines="1"
android:text="@={shipper.getPrice(priceKey)}"/>
Run Code Online (Sandbox Code Playgroud)
android:text =“ @ = {shipper.getPrice(priceKey)}”
构建过程中的编译器错误表明我们应该使用@InverseMethod批注。我尝试这样的事情:
@InverseMethod(value = "getPrice")
@Override
public void setPrice(Object key, int price) {
super.setPrice(key, price);
}
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,我有下一个错误。
error: @InverseMethods must have a non-void return type
Run Code Online (Sandbox Code Playgroud)
因此,我很高兴在这里对整个流程进行很好的解释。谢谢
我有一个进度条和一个文本字段,两者都根据彼此的输入进行更新:
class ViewModel: ObservableObject {
@Published var progressBarValue: Double {
didSet {
textFieldValue = String(progressBarValue)
}
}
@Published var textFieldValue: String {
didSet {
progressBarValue = Double(progressBarValue)
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于更新一个更新另一个,我最终在我的代码中有无限递归。
有没有办法用Combine 或普通的swift 代码来解决这个问题?
我是android数据绑定的新手。我想将多个 SeekBars 绑定到一个浮点数集合,例如
SeekBar1.progress <---> modelArray[0]
SeekBar2.progress <---> modelArray[1]
...
Run Code Online (Sandbox Code Playgroud)
由于SeekBar的进度是Int类型,我觉得用Converter会更好,下面是转换器代码:
import android.databinding.InverseMethod
import android.util.Log
import kotlin.math.roundToInt
class Converter {
@InverseMethod("progressToFloat")
fun floatToProgress(value: Float): Int {
val result = (value * 100.0f).roundToInt()
Log.i("MODEL", "convert $value to $result(Int)")
return result
}
fun progressToFloat(value: Int): Float {
return value.toFloat()/100.0f
}
}
Run Code Online (Sandbox Code Playgroud)
模型结构如下所示:
class Model {
val params by lazy {
ObservableArrayMap<Int, Float>()
}
//....
}
Run Code Online (Sandbox Code Playgroud)
我的xml如下:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data class="MyBinding">
<variable name="Converter" type="com.mypackage.model.Converter"></variable>
<variable
name="Converter"
type="com.mypackage.model.Converter"></variable>
<variable
name="model"
type="com.mypackage.model.Model"></variable>
</data> …
Run Code Online (Sandbox Code Playgroud) data-binding android kotlin android-databinding two-way-binding
据我所知,在 Angular 中,以下两行在功能上是相同的
属性和事件绑定
<input type="text" [value]="customerName" (input)="customerName= $event.target.value" />
Run Code Online (Sandbox Code Playgroud)
双向数据绑定
<input type="text" [(ngModel)]="customerName' />
Run Code Online (Sandbox Code Playgroud)
让我困惑的是,Angular 如何通过双向数据绑定知道绑定到元素的输入 ,而不是元素支持的任何其他事件?event
input
input
在第一个示例中,代码专门定义了要绑定到哪个事件(“输入” event
),但在第二个示例中则没有。Angular 如何选择要绑定的事件?
谁能告诉我如何将组件的 prop 绑定到它自己的数据属性?例如,假设我有一个名为ModalComponent
<template> // ModalComponent.vue
<div v-if="showModal">
...
<button @click="showModal = !showModal">close the modal internally</button>
</div>
</template>
<script>
export default {
props: {
show: {
type: Boolean,
default: false
},
},
data() {
return {
showModal: false,
}
}
}
</script>
<style>
</style>
Run Code Online (Sandbox Code Playgroud)
现在考虑我使用此模式作为父组件内部的可重用组件,使用外部按钮打开模式的弹出窗口。
<template>
<button @click="showChild = !showChild">click to open modal</button>
<ModalComponent :show="showChild" />
</template>
<script>
export default {
components: {ModalComponent},
data() {
return {
showChild: false,
}
}
}
</script>
<style>
</style>
Run Code Online (Sandbox Code Playgroud)
如何使每次父母单击按钮时,通过将本地绑定showModal
到 prop …