我有一个看起来像这样的设置:
// myDG is a DataGrid whose columns are DataGridTextColumn
ObservableCollection<MyItem> myOC;
// myOC is populated with some new MyItem
myDG.ItemsSource = myOC;
Run Code Online (Sandbox Code Playgroud)
其中MyItem工具INotifyPropertyChanged.当用户将值输入单元格时,正确捕获的方法是什么?
我试着追赶PropertyChanged的MyItemS,但我在后台周期性也更新值(这个想法是,当用户手动编辑值,标志被触发,告诉周期性的计算以避免覆盖手动输入的数据).所以PropertyChanged抓住了一切,包括我不想要的定期更新.我想可以使这个工作(通过在我进行定期计算时设置一个标志,然后在PropertyChanged事件处理程序上检查是否缺少标志 - 但我想知道是否有更简单的解决方案.)
我尝试过捕获,myDG.CurrentCellChanged但每次用户更改单元格选择时都会触发,而不是在编辑单元格内容时.
编辑:这是XAML:
<DataGrid x:Name="myDG" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="10,10,182,0" VerticalAlignment="Top" Height="329" ClipboardCopyMode="IncludeHeader">
<DataGrid.Columns>
<DataGridTextColumn Header="Col1" Binding="{Binding Prop1}" IsReadOnly="True"/>
<DataGridTextColumn Header="Col2" Binding="{Binding Prop2}" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
这是MyItem实现(使用Fody/PropertyChanged):
[ImplementPropertyChanged]
class MyItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public …Run Code Online (Sandbox Code Playgroud) 以下代码生成语法错误:
class Foo
{
public Action a = () => { };
}
void doSomething()
{
var foo = new Foo();
(foo.a)(); // error CS1525: Invalid expression term ')'
}
Run Code Online (Sandbox Code Playgroud)
但是,以下替代方案都有效:
foo.a(); // works
Action a = foo.a; a(); // works
Run Code Online (Sandbox Code Playgroud)
为什么会这样?(foo.a)是一个Action; 为什么我不能打电话呢?
假设我有一个dword,我想用十六进制输出std :: cout和left-pad with zero,所以0xabcd将显示为0x0000abcd.看起来你必须这样做:
uint32_t my_int = 0xabcd;
std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0')
<< my_int << std::endl;
Run Code Online (Sandbox Code Playgroud)
对于可以在C中完成的事情来说,这似乎很荒谬printf("0x%08X\n", my_int);.有没有办法让这个更短,同时仍然使用std :: cout输出(除了使用命名空间std)?
以下代码无法编译(Godbolt 链接):
#include <concepts>
template <class Fn>
decltype(auto) g(Fn&& fn) { return fn(); }
template <typename T>
requires(std::integral<T>) int f() { return 0; }
template <typename T>
int f() { return 1; }
int main() {
f<int>();
f<void>();
g(f<int>); // error: invalid initialization of non-const reference of type 'int (&)()'
// from an rvalue of type '<unresolved overloaded function type>'
g(f<void>);
}
Run Code Online (Sandbox Code Playgroud)
对我来说,似乎出乎意料的是,调用时重载解析会成功f<int>()(选择受约束的版本作为比不受约束的版本更好的匹配),但作为f<int>参数传递时会失败。
请注意,将无约束版本更改为不相交约束确实可以编译(Godbolt 链接):
#include <concepts>
template <class Fn>
decltype(auto) g(Fn&& …Run Code Online (Sandbox Code Playgroud) 我正在尝试为矩阵的每一列取累积和.这是我在R中的代码:
testMatrix = matrix(1:65536, ncol=256);
microbenchmark(apply(testMatrix, 2, cumsum), times=100L);
Unit: milliseconds
expr min lq mean median uq max neval
apply(testMatrix, 2, cumsum) 1.599051 1.766112 2.329932 2.15326 2.221538 93.84911 10000
Run Code Online (Sandbox Code Playgroud)
我用Rcpp进行比较:
cppFunction('NumericMatrix apply_cumsum_col(NumericMatrix m) {
for (int j = 0; j < m.ncol(); ++j) {
for (int i = 1; i < m.nrow(); ++i) {
m(i, j) += m(i - 1, j);
}
}
return m;
}');
microbenchmark(apply_cumsum_col(testMatrix), times=10000L);
Unit: microseconds
expr min lq mean median uq max neval
apply_cumsum_col(testMatrix) 205.833 …Run Code Online (Sandbox Code Playgroud) 我有一个类需要NotifyPropertyChanged在任何属性更改时调用方法.我在例子中看到的是:
private string property1_;
public string Property1
{
get { return property1_; }
set { property1_ = value; NotifyPropertyChanged(); }
}
private string property2_;
public string Property2
{
get { return property2_; }
set { property2_ = value; NotifyPropertyChanged(); }
}
// .......
Run Code Online (Sandbox Code Playgroud)
这真的是惯用的方式吗?对于我想要添加的每个属性,它需要几行样板.此外,如果我想更改属性和字段的名称,我需要更改4个不同的东西.这似乎违反了DRY.
这主要是一种理解检查,因为我找不到关于这个主题的完整参考.
在C#中,当我写的时候readonly Foo myFoo,我基本上是说myFoo是一个指针Foo,并且指针不能被重新分配.为了保证底层Foo不能被重新分配,我需要一个完整的其他类或接口ImmutableFoo.
现在考虑构造List<Foo>.它基本上是指向指针列表的指针Foo,即类似于vector<Foo *> *C++.有三个地方可以放入constC++.
const vector<const Foo *> * const
Run Code Online (Sandbox Code Playgroud)
所以我认为每个相当于,
List<Foo> = vector<Foo *> * // No consts
ReadOnlyCollection<Foo> = const vector<Foo *> * // First const toggled
List<ImmutableFoo> = vector<const Foo *> * // Second const toggled
readonly List<Foo> = vector<Foo *> * const // Third const toggled
readonly ReadOnlyCollection<ImmutableFoo>
= const …Run Code Online (Sandbox Code Playgroud) 说我初始化vector<vector<string>>就像这样:
vector<vector<string>> v;
v = {{
{"a", "b", "c"},
{"aa", "bb"},
{"xyz", "yzx", "zxy"},
{}
}};
Run Code Online (Sandbox Code Playgroud)
现在假设我想将已经存在的东西附加vector<string>到某些v元素中.像这样:
vector<string> suffix {{"1", "2", "3"}};
vector<vector<string>> v;
v = {{
{"a", "b", "c"} + suffix,
{"aa", "bb"},
{"xyz", "yzx", "zxy"} + suffix,
{}
}};
Run Code Online (Sandbox Code Playgroud)
该语法显然不起作用,因为operator+没有以这种方式定义.
我知道可以构建v第一种方式然后编写
vector<int> indices = {0, 2};
for(int i: indices)
v[i].insert(v[i].end(), suffix.begin(), suffix.end());
Run Code Online (Sandbox Code Playgroud)
但这不方便,因为我可能有几个suffix附加到任意的向量v[i].我希望它suffix与初始化一起,v[i]所以它是有道理的,如果我从v初始化添加/删除元素,我不必转移索引.
在gcc中这很好用.代码类似于:
unsigned char b[50] = "\xda\xd1 ... \x0"; //some shellcode with terminating \x0
( (void(*)())b )(); //cast b to function pointer from void to void, then run it
Run Code Online (Sandbox Code Playgroud)
但是当它放在Visual C++中时,它会吐出这个错误消息:
1>..\test.cpp(132): error C2440: 'type cast' : cannot convert from 'unsigned char [50]' to 'void (__cdecl *)(void)'
1> There is no context in which this conversion is possible
Run Code Online (Sandbox Code Playgroud)
谁知道为什么会这样?
这是代码:
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
class Date
{
public:
Date(int year, int month, int day) : year(year), month(month), day(day) {}
Date(const Date &d) : year(d.year), month(d.month), day(d.day) {}
std::string to_string() {
std::stringstream ss;
ss << std::setfill('0') << std::setw(4) << year << '-' << std::setw(2) << month << '-' << day;
return ss.str();
}
private:
int year, month, day;
};
int main()
{
std::vector<Date> vd;
vd.emplace_back(2017, 1, 13);
vd.emplace_back(vd[0]);
std::cout << vd.back().to_string() << "\n";
} …Run Code Online (Sandbox Code Playgroud) c++ ×6
c# ×4
c++11 ×2
c++-concepts ×1
c++20 ×1
casting ×1
const ×1
cout ×1
datagrid ×1
dry ×1
iostream ×1
overloading ×1
printf ×1
properties ×1
r ×1
rcpp ×1
shellcode ×1
vector ×1
visual-c++ ×1
wpf ×1