【问题标题】:In C# and WPF, can you bind an element of an array to an objects property?在 C# 和 WPF 中,您可以将数组的元素绑定到对象属性吗?
【发布时间】:2011-04-29 11:42:21
【问题描述】:

例如,是否可以将 Textblock 的 Text 属性绑定到 String 类型的元素 Name[2] 上?

【问题讨论】:

  • 您的意思是如何在 XAML 中以声明方式将 Name[2] 绑定到控件属性?
  • 是的,这就是我的意思。现在,虽然绑定正在工作,但目标控件在源变量更改时不会更新。我被难住了

标签: c# .net wpf data-binding mvvm


【解决方案1】:

我不确定您所说的确切含义:String 类型的元素 Name[2],因此这里有两种可能的解决方案来解决您的问题:Array1 和 String1。 Array1 显示绑定到数组元素的弓,String1 显示如何显示字符串中的单个字符。

代码:

 public partial class MainWindow : Window
{
    private Array array1 = new[] {"test1", "test2", "test3"};
    public Array Array1 { get { return array1; } }

    public string string1 = "string";
    public string String1 { get { return string1; } }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
}

XAML:

 <StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Array1[0]}"/>
    <TextBlock Text="{Binding Array1[2]}"/>
    <TextBlock Text="{Binding String1[0]}"/>
    <TextBlock Text="{Binding String1[1]}"/>
</StackPanel>

希望对您有所帮助。

【讨论】:

  • 谢谢,这行得通,只是更新源不会更新目标控件。我已经尝试了一切,但到目前为止还没有工作。我应该使用 ObservableCollection 吗?
  • 由于此评论不允许我发布另一个答案。
【解决方案2】:

是的,你可以。以下是 XAML 方法。 如果要在值更改时自动更新 UI,建议绑定到 Observable 集合。

public class DataStub
{
    public Array SomeNorthEasternStates
    {
        get
        {
            return new[] { "NH", "VT", "CT", "MA", "ME" };      
        }
    }
}

XAML:假设 DataContext 设置正确:

<TextBox Margin="5" Text="{Binding SomeNorthEasternStates[3], Mode=Default}"/>

【讨论】:

  • 您使用Array 类的方法而不是简单地使用var testArray = new string[5]; testarray[0] = "NY"; ... 是否有特殊原因。
  • @Heinzi - 或者 var testArray = new [] { "NH", "VT", "CT", "MA", "ME" }; (纽约和新泽西不在新英格兰):)
  • @John Bowen。我的错。改名了。
  • @Heinzi。没有具体原因。请参阅编辑。 7 小时后,睡了一会儿,喝了杯咖啡,我想知道我为什么要那样做。 :)
【解决方案3】:

我在 xaml 中添加了一个按钮并订阅了“点击”事件。

这里是 C# 代码。

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private Array array1 = new[] { "test1", "test2", "test3" };
    public Array Array1 { get { return array1; } }


    public string string1 = "string";
    public string String1
    {
        get { return string1; }
        set
        {
            string1 = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("String1"));
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        String1 = DateTime.Now.ToString();
        array1.SetValue("another test", 0); 
        PropertyChanged(this, new PropertyChangedEventArgs("Array1"));
    }
}

【讨论】:

  • 谢谢。 ObservableCollection 也实现了这些功能并且运行良好。感谢您的帮助 klm_。
【解决方案4】:

如果你的意思是如果我们可以将数组项与文本框绑定,那么,是的

 <TextBox Margin="10" Text="{Binding Name[2], Mode=Default}"  Name="textBox1"/>

【讨论】:

  • TextBox 没有 Content 属性。
【解决方案5】:

改用 ObservableCollection:

private ObservableCollection<string> _myItems = new ObservableCollection<string>(new[] { "test1", "test2", "test3" });

public ObservableCollection<string> MyItems
{
    get { return _myItems; }
    set { _myItems = value; }
}

Xaml

    <StackPanel Orientation="Vertical">
        <TextBox Text="{Binding MyItems[0]}"/>
        <TextBox Text="{Binding MyItems[2]}"/>
        <TextBlock Text="{Binding MyItems[0]}"/>
        <TextBlock Text="{Binding MyItems[1]}"/>
    </StackPanel>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2011-05-05
    • 2018-05-10
    • 2018-07-13
    • 2010-09-08
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多