我试图制作一个显示星期几和工作日开始/结束的应用程序作为练习。
所以我用这些元素和绑定制作了XAML:
<StackPanel Grid.Row="1" Margin="0,0,0,0" >
<ListView ItemsSource="{Binding TimeListView}">
<ListView.View>
<GridView>
<GridViewColumn Width="200" Header="Day"
DisplayMemberBinding="{Binding Date}" />
<GridViewColumn Width="200" Header="Time"
DisplayMemberBinding="{Binding Time}" />
<GridViewColumn Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button x:Name="EditEntry" Content="Edit" Click="EditClick" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
在我的EditClick中我有这段代码:
private void EditClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("Sender: " + sender.ToString());
}
所以我的问题是:如何在EditClick函数的同一行中保存日期和时间的数据?
答案 0 :(得分:2)
最简单的方法是使用发件人按钮的DataContext property。它将包含绑定到current("parent")行的项目:
public MainWindow()
{
InitializeComponent();
this.TimeListView = new ObservableCollection<Item>(new Item[]
{
new Item(),
new Item()
});
this.DataContext = this;
}
public ObservableCollection<Item> TimeListView
{
get;
private set;
}
private void EditClick(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var item = (Item)button.DataContext;
MessageBox.Show("Item is: " + item.ToString());
}
项目类:
public class Item
{
public Item()
{
this.Day = DateTime.Now.DayOfWeek;
this.Time = DateTime.Now.TimeOfDay;
}
public DayOfWeek Day { get; set; }
public TimeSpan Time { get; set; }
public override string ToString()
{
return String.Format("{0}, {1}", this.Day, this.Time);
}
}
答案 1 :(得分:1)
好的,如果我正确地解释了这个问题,那么当调用EditClick函数时,你想获得当前选择的日期和时间吗?
如果是这样,请执行:
string theCurrentSelectedDateTime = YouListView.ColumnOne.SelectedItem.ToString() + YourListView.ColumnTwo.SelectedItem.ToString();
那应该能得到你想要的东西。但如果你问其他什么,那就评论一下,因为我还不准评论:(。