标签: data-binding

如何通过ObjectDataProvider将ComboBox绑定到通用字典

我想用代码中的键/值数据填充ComboBox,我有这个:

XAML:

<Window x:Class="TestCombo234.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestCombo234"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="Choices" ObjectType="{x:Type local:CollectionData}" MethodName="GetChoices"/>
    </Window.Resources>
    <StackPanel HorizontalAlignment="Left">
        <ComboBox ItemsSource="{Binding Source={StaticResource Choices}}"/>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后:

using System.Windows;
using System.Collections.Generic;

namespace TestCombo234
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public static class CollectionData
    {
        public static Dictionary<int, string> GetChoices()
        {
            Dictionary<int, string> choices = new Dictionary<int, string>();
            choices.Add(1, "monthly");
            choices.Add(2, "quarterly");
            choices.Add(3, "biannually");
            choices.Add(4, "yearly");
            return choices;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这给了我这个:

alt text …

c# data-binding wpf xaml combobox

46
推荐指数
2
解决办法
6万
查看次数

android数据绑定与自定义视图

所述的Android数据绑定导向讨论了一个活动或片段内结合的值,但是有执行数据与自定义视图结合的方法吗?

我想做的事情如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.mypath.MyCustomView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="40dp"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

my_custom_view.xml:

<layout>

<data>
    <variable
        name="myViewModel"
        type="com.mypath.MyViewModelObject" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{myViewModel.myText}" />

</LinearLayout>

</layout>
Run Code Online (Sandbox Code Playgroud)

虽然看起来可以通过在自定义视图上设置自定义属性来实现这一点,但如果要绑定很多值,这很快就会变得很麻烦.

有没有一个好方法来完成我想要做的事情?

data-binding android custom-view

45
推荐指数
5
解决办法
3万
查看次数

Angular2组件@Input双向绑定

我有一个数据驱动的Angular应用程序.我有一个切换组件,我传递切换状态.我的问题是双向数据绑定似乎不起作用,除非我将toggle布尔值作为对象传递.有没有办法让它在不使用EventEmitter或将变量作为对象传递的情况下工作.这是一个可重用的组件,应用程序是大量数据驱动的,因此将值作为对象传递给非选项.我的代码是......

toggle.html

<input type="checkbox" [(ngModel)]="toggled" [id]="toggleId" name="check"/>
Run Code Online (Sandbox Code Playgroud)

toggle.component.ts

import { Component, Input, EventEmitter, Output } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'toggle-switch',
  templateUrl: 'toggle-switch.component.html',
  styleUrls: ['toggle-switch.component.css']
})

export class ToggleSwitchComponent {

  @Input() toggleId: string;
  @Input() toggled: boolean;

}
Run Code Online (Sandbox Code Playgroud)

parent.component.html

<toggle-switch toggleId="toggle-1" [(toggled)]="nongenericObject.toggled"></toggle-switch>
Run Code Online (Sandbox Code Playgroud)

data-binding components decorator typescript angular

45
推荐指数
2
解决办法
2万
查看次数

针对类型安全数据绑定缺少C#中"nameof"运算符的解决方法?

nameof在C#中包含运算符的情绪很多.作为此运算符如何工作的示例,nameof(Customer.Name)将返回字符串"Name".

我有一个域对象.我必须绑定它.然后我需要属性的名称作为字符串.我希望它们是类型安全的.

我记得在.NET 3.5中遇到过一种解决方法,它提供了nameoflambda表达式的功能和涉及的lambda表达式.但是,我无法找到此解决方法.任何人都可以为我提供这种解决方法吗?

我也对nameof如果可能的话在.NET 2.0中实现功能的方法感兴趣.

.net c# data-binding .net-3.5 .net-2.0

44
推荐指数
2
解决办法
2万
查看次数

如何将List <CustomObject>绑定到WPF DataGrid?

我是WPF的新手,想要做一些基本的数据绑定.我有一个CustomObject的列表,并希望将其绑定到DataGrid.

MainWindow.xaml.cs

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                List<ArticleItem> list = new List<ArticleItem>() 
                {
                new ArticleItem(){ ID=3, Title="test", ViewCount=5},
                new ArticleItem(){ ID=3, Title="test", ViewCount=5},
                new ArticleItem(){ ID=3, Title="test", ViewCount=5},
                new ArticleItem(){ ID=3, Title="test", ViewCount=5},
                };
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf datagrid

44
推荐指数
3
解决办法
11万
查看次数

使用DataBinding库绑定事件

我正在尝试使用Android M附带的DataBinding Library将事件与xml中的视图绑定.我正在关注Android开发人员的示例并逐步实现.对于视图的属性,如可见性,文本工作正常,但如果我尝试与onclick绑定,它不会按预期工作.这是我到目前为止尝试过的示例代码:

 <data>
    <import type="android.view.View"/>
    <variable name="user" type="com.example.databinding.User"/>
    <variable name="handlers" type="com.example.databinding.MyHandlers"/>
</data>

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{user.firstName}"
    android:visibility="@{user.isFriend ? View.VISIBLE : View.GONE}" />
 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me"
    android:id="@+id/button"
    android:layout_gravity="left"
    android:onClick="@{handlers.onClickFriend}"/>
Run Code Online (Sandbox Code Playgroud)

主要活动 :

  public class MainActivity extends AppCompatActivity {

  User user;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = 
    DataBindingUtil.setContentView(this,R.layout.activity_main);
    user = new User("Pankaj","Kumar",true,true);
    binding.setUser(user);
   }
 }
Run Code Online (Sandbox Code Playgroud)

MyHandlers:

public class MyHandlers {
public void onClickFriend(View view){
    Log.i(MyHandlers.class.getSimpleName(),"Now Friend");
}

public void onClickEnemy(View …
Run Code Online (Sandbox Code Playgroud)

data-binding android android-databinding android-6.0-marshmallow

44
推荐指数
3
解决办法
4万
查看次数

如何在Dialog中使用数据绑定?

我在Dialog中实现数据绑定时遇到了麻烦.可能吗?

下面是我的xml.

<data>

    <variable
        name="olaBooking"
        type="com.example.myapp.viewmodels.ViewModel" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/cv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:elevation="4dp"
        android:padding="15dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:gravity="center"
                android:padding="15dp"
                android:text="OLA Cab Booked !"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/colorPrimaryDark" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start|center"
                android:padding="15dp"
                android:text="Car Details" />

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/colorPrimaryDark" />

            <TextView
                android:id="@+id/driverName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@{olaBooking.driverName}" />

            <TextView
                android:id="@+id/carModel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@{olaBooking.getCarName}" />

            <TextView
                android:id="@+id/carNo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@{olaBooking.getCabNo}" />

            <TextView
                android:id="@+id/eta"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@{olaBooking.getEta}" /> …
Run Code Online (Sandbox Code Playgroud)

data-binding android mvvm android-databinding

44
推荐指数
5
解决办法
3万
查看次数

无法解析Android数据绑定类

使用数据绑定时,我无法MainActivityBinding 按照数据绑定指南获取类

我的布局名称是activity_main.xml.我也看到了Android - DataBinding - 如何以及何时生成Binding类?但它无法帮助我.

data-binding android android-databinding

44
推荐指数
10
解决办法
5万
查看次数

TextBox绑定TwoWay不会更新,直到焦点丢失WP7

我有一个页面,其中包含一些用于数据输入的文本框.文本框的绑定设置为TwoWay.如果文本框失去焦点,则视图模型中的数据仅会更新.如果单击某个按钮(例如"保存"),并且文本框仍具有焦点,则在保存事件的视图模型中不会更改文本框中的更改.

有没有办法让绑定在失去焦点之前保存文本框的值?或者我是否需要在保存事件中执行某些操作?

data-binding silverlight xaml windows-phone-7

43
推荐指数
6
解决办法
2万
查看次数

为什么绑定更新没有实现INotifyPropertyChanged?

我创建了一个ViewModel,并将其属性绑定到UI上的两个文本框.当我更改first的值并将焦点从文本框中移出但是我没有实现INotifyPropertyChanged 时,另一个文本框的值会发生变化.这是怎么回事?

以下是XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <StackPanel>
        <TextBox Text="{Binding Name}" />
        <TextBox Text="{Binding Name}" />
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

以下是我的ViewModel

class ViewModel
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

data-binding wpf xaml

42
推荐指数
1
解决办法
8510
查看次数