【问题标题】:Set textbox value when another change in a listview当列表视图中的另一个更改时设置文本框值
【发布时间】:2015-06-01 09:49:46
【问题描述】:

我有一个包含 Hour(对象)的 ObservableCollection。在里面,我有一个 Title 和一个 Value 属性。

在我看来,我有一个列表视图,绑定在这个集合上。 Title 是文本块,Value 是文本框(用户可以输入文本)。

我想在一次更改时更改所有文本框(值)的内容。 一点代码:

public class Hour : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public string Title { get; set; }

        private int valueContent;
        public int Value
        {
            get { return valueContent; }
            set 
            {
                valueContent = value;
                NotifyPropertyChanged("Value");
            }
        }
}

我的可观察集合:

private ObservableCollection<Hour> hours;
public ObservableCollection<Hour> Hours
{
    get { return hours; }
    set
    {
        hours= value;
        NotifyPropertyChanged("Hours");
    }
}

xaml:

<ListBox Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="3" Grid.RowSpan="3" ItemsSource="{Binding Hours, Mode=TwoWay}" SelectedItem="{Binding SelectedHour,Mode=TwoWay}" ItemTemplate="{StaticResource HourTemplate}" />
<DataTemplate x:Key="HourTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Title}" FontSize="18" Width="150" />
        <TextBox Text="{Binding Value, Mode=TwoWay}" FontSize="15" Width="150" TextChanged="TextBox_TextChanged" />
    </StackPanel>
</DataTemplate>

所以,我会举个例子:

Title           -          Value
08h00           -           0
09h00           -           0
10h00           -           0
11h00           -           0
12h00           -           0

我希望,当我更改一个值(例如:10h00)时,该值之后的所有值都更改为 10h00 的值。 这是预期的结果:

Title           -          Value
08h00           -           0
09h00           -           0
10h00           -           1 <--- change here
11h00           -           1 <--- change because 10h00 changed
12h00           -           1 <--- change because 10h00 changed

感谢您的帮助。

【问题讨论】:

    标签: c# wpf listview binding


    【解决方案1】:

    没有任何干净方法可以做到这一点。

    我将首先向HourValueUpdated 添加一个事件。在Value 的设置器中引发该事件,并让视图模型for 每个Hour 对象监听它。让事件将发送者作为参数传递,例如:

    public event Action<Hour> ValueUpdated;
    
    //When raising
    var handler = ValueUpdated;
    if (handler != null)
       handler(this);
    

    现在在视图模型处理程序中,您需要找到发送者的索引,然后将更改应用到它之后的每个小时。

    private void HandleValueUpdate(Hour sender)
    {
       int senderIndex = allItems.IndexOf(sender);
       IEnumerable<Hour> subsequentHours = allItems.Skip(senderIndex + 1);
    
       foreach (Hour h in subsequentHours)
       {
           h.SetValue(sender.Value);
       }
    }
    

    您可能希望在不引发ValueUpdated 事件的情况下找到一种方法,因为这样做不会很有效。我通过调用函数而不是设置属性来建模,但如何做取决于你。

    【讨论】:

    • 谢谢伙计!我有一个无限循环,只需要检查 Value 的设置时间,如果它还没有更新。但现在可以了。
    • @carndacier 是的,如果你只是设置属性,你最终会得到很多个额外的函数调用。很高兴你成功了!
    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 2020-12-14
    • 2017-06-19
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多