小编A.R*_*.R.的帖子

从ItemsControl中的模板中获取项目

我有一个ItemsControl,其中填充了一些ViewModel类的可观察集合,如下所示:

<ItemsControl ItemsSource="{Binding MyCollection}">
  <ItemsControl.ItemTemplate>
    <DataTemplate Type="{x:Type local:MyViewModel}">
      <Button Content="{Binding ActionName}" Click="ClickHandler"/>
    </DataTemplate>
  <ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

效果很好,看起来很棒,但我似乎无法弄清楚如何让"ClickHandler"知道由数据模板表示的类"MyViewModel".看哪!

private void ClickHandler(object sender, RoutedEventArgs e)
{
  // The 'sender' is the button that raised the event.  Great!
  // Now how do I figure out the class (MyViewModel) instance that goes with this button?
}
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml templates

2
推荐指数
1
解决办法
3787
查看次数

方法的C#默认值 - 编译错误:编译时常量

private static Vector2 DefaultMulFactors = new Vector2(0.5f, 0.5f);
private static Point DefaultShifts = new Point(0,0);

public static Vector2 Function(Vector2? mulFactors = MyClass.DefaultMulFactors , Point? shifts = MyClass.DefaultShifts  )
{ 
    ...
    return result;
}
Run Code Online (Sandbox Code Playgroud)

为什么我的代码不接受我的静态值?如何为我的函数参数指定默认参数?确实Vector2? mulFactors = new Vector(0.2,0.3)Vector2? mulFactors = Vector2.Zero不起作用.

c# xna default-value

1
推荐指数
1
解决办法
1917
查看次数

R seq 函数参数太多?

我收到一个我根本不明白的错误。我只是在生成一些序列时遇到了这个问题:

这应该创建一个包含 50 个数字的序列。

seq.int(from=1,to=1000,by=5,length.out=50)
Run Code Online (Sandbox Code Playgroud)

但如果我在控制台中输入此内容,我会收到错误消息:

seq.int(from = 1, to = 1000, by = 5, length.out = 50) 中的错误:
参数太多

如果我查看帮助 ( ?seq),在用法部分中有这样一行,这使得我看起来好像我正确地调用了该函数,并且它允许这么多数量的参数:

seq.int(从、到、by、length.out、沿.with、...)

那么到底是怎么回事呢?我错过了一些基本的东西,还是文档已经过时了?

注意 我在代码示例中向函数提供的参数只是为了示例。我并不是想解决特定问题,只是好奇为什么会出现错误。

r sequence

1
推荐指数
1
解决办法
4875
查看次数

三元运营商和维护

程序员A喜欢一些三元运算符,尤其是嵌套运算符.更好的是,他声称它们使代码更易于阅读和维护,因为它们可以适应更少的线路.程序员B认为,当三元嵌套时可读性会丢失,反过来代码变得难以维护.

看看下面的块:

private int MyFunction(bool condA, bool condB)
{
  // Programmer A says:
  // Nested ternary --> This is easier to read and maintain because it is on one line.  Ternaries can be nested 4 or 5 deep and it is no problem.
  return condA ? condB ? 20 : -10 : -20;

  // Programmer B says:
  // Unwrapped --> This is more readable as it better describes the step by step evaluation of the conditions and their results.
  if …
Run Code Online (Sandbox Code Playgroud)

c# maintenance code-readability

0
推荐指数
1
解决办法
1303
查看次数

标签 统计

c# ×3

code-readability ×1

default-value ×1

maintenance ×1

r ×1

sequence ×1

templates ×1

wpf ×1

xaml ×1

xna ×1