当列表视图中的另一个更改时设置文本框值
Set textbox value when another change in a listview
我有一个包含 Hour (object) 的 ObservableCollection。在里面,我有一个 Title 和一个 Value 属性。
在我看来,我有一个列表视图,绑定到这个 collection。标题是一个文本块,值是一个文本框(用户可以输入文本)。
我想在更改时更改所有文本框(值)的内容。
一点代码:
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");
}
}
}
我的观察collection :
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
感谢您的帮助。
没有任何干净的方法可以做到这一点。
我将从向 Hour
class、ValueUpdated
添加一个事件开始。在 setter 中为 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
事件,因为如果这样做,效率不会很高。我通过调用一个函数而不是设置 属性 来建模,但是如何操作取决于您。
我有一个包含 Hour (object) 的 ObservableCollection。在里面,我有一个 Title 和一个 Value 属性。
在我看来,我有一个列表视图,绑定到这个 collection。标题是一个文本块,值是一个文本框(用户可以输入文本)。
我想在更改时更改所有文本框(值)的内容。 一点代码:
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");
}
}
}
我的观察collection :
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
感谢您的帮助。
没有任何干净的方法可以做到这一点。
我将从向 Hour
class、ValueUpdated
添加一个事件开始。在 setter 中为 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
事件,因为如果这样做,效率不会很高。我通过调用一个函数而不是设置 属性 来建模,但是如何操作取决于您。