小编Stu*_*urm的帖子

如何使用LINQ计算与条件匹配的元素数

我尝试了很多东西,但对我来说最符合逻辑的是这个:

int divisor = AllMyControls.Take(p => p.IsActiveUserControlChecked).Count();
Run Code Online (Sandbox Code Playgroud)

AllMyControls是一家集的UserControls,我想知道的是有多少UserControlsIsActiveUserControlChecked属性设置为true.

我在VS中获得的是:

Cannot convert lambda expression to type 'int' because it is not a delegate type
Run Code Online (Sandbox Code Playgroud)

我的表情有什么问题?

c# linq count

23
推荐指数
2
解决办法
5万
查看次数

如何设置checkbox.isChecked而不引发事件

有没有办法检查CheckBox没有运行与检查相关的代码?只是为了视觉外观.

编辑:

private void normalCheck_Checked(object sender, RoutedEventArgs e)
{
    normal();
}
Run Code Online (Sandbox Code Playgroud)

想象一下,我想设置normalCheckBox.IsChecked=true;但不提高事件.那可能吗?

c# wpf checkbox

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

给定其属性的列表中的项目索引

MyList List<Person>有可能是Person与它的Name属性设置为"ComTruise".我需要该MyList列表中的索引.不是Person,只是它的索引.

我现在正在做的是:

string myName = ComTruise;
int thatIndex = MyList.SkipWhile(p => p.Name != myName).Count();
Run Code Online (Sandbox Code Playgroud)

如何更好地完成或使用IndexOf方法执行相同的任务?

c# linq

16
推荐指数
3
解决办法
2万
查看次数

性能:List.Count与检查存储的变量

我想知道这是否有所不同:

 for (int i = 0; i < values.Count; i++)
        {
            //
        }
Run Code Online (Sandbox Code Playgroud)

VS

int num = values.Count;

for(int=0; i<num; i++)
{

}
Run Code Online (Sandbox Code Playgroud)

我认为第二种方法更好,因为您不需要计算每次迭代中的所有项目.但我可能错了.有人可以照亮我吗?

c# performance loops count

15
推荐指数
2
解决办法
6178
查看次数

获取给定类型的集合的引用

我有几个词典:

Dictionary<int, Type1> Type1Dictionary { get; set; }
Dictionary<int, Type2> Type2Dictionary { get; set; }
Dictionary<int, Type3> Type3Dictionary { get; set; }
Dictionary<int, Type4> Type4Dictionary { get; set; }
Run Code Online (Sandbox Code Playgroud)

哪里Typei (i = 1..4)派生自相同的基类(BaseType).我想要一个方法,返回给定类型的字典的引用.稍后,我将在该字典上执行一些操作,如添加或删除:

Type1 example = new Type1(); 
var dic = GetDictionary(example);
dic.Add(example.ID, example);
Run Code Online (Sandbox Code Playgroud)

注意:我不想将我的词典设置为 Dictionary<int, BaseType>

我可以写这样的东西,但不会返回对字典的引用:

Dictionary<int, BaseType> GetDictionary(BaseType myObject)
{
    var dic = new Dictionary<int, BaseType>();
    if(myObject is Type1)
    {
    //ideally I would return my Type1Dictionary here but I can't due type incompatibility …
Run Code Online (Sandbox Code Playgroud)

c#

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

Josh Smith MVVM演示应用

MainWindow,我们有:

 <HeaderedContentControl 
          Content="{Binding Path=Workspaces}"
          ContentTemplate="{StaticResource WorkspacesTemplate}"
          Header="Workspaces"
          Style="{StaticResource MainHCCStyle}"
          />
Run Code Online (Sandbox Code Playgroud)

资源中:

  <DataTemplate x:Key="WorkspacesTemplate">
    <TabControl 
      IsSynchronizedWithCurrentItem="True" 
      ItemsSource="{Binding}" 
      ItemTemplate="{StaticResource ClosableTabItemTemplate}"
      Margin="4"
      />
  </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

文章中说:

类型化的DataTemplate没有分配给它的x:Key值,但它的DataType属性设置为Type类的实例.如果WPF尝试呈现您的一个ViewModel对象,它将检查资源系统是否在作用域中具有类型化的DataTemplate,其DataType与ViewModel对象类型的(或基类)相同.如果找到一个,它将使用该模板呈现选项卡项的Content属性引用的ViewModel对象.

我的问题是:

模板如何知道类型是工作空间的集合(WorkspaceViewModel)?

c# datatemplate mvvm

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

如何使用TrueForAll

我有一个bools列表,我想检查每个是否设置为true.我可以运行循环并检查它,但我想尝试TrueForAll使用列表的方法.我需要一个谓词,但我找不到这样一个简单任务的明确例子.

c# list

6
推荐指数
2
解决办法
9407
查看次数

MouseOver上的Gridsplitter visibile

我希望GridSplitter只有当用户将鼠标放在它上面时才能看到它.为此我正在设计DataTrigger它的风格.但我无法弄清楚它是什么错,因为我没有得到理想的行为.它保持不变.

                <GridSplitter
            ResizeDirection="Columns"
            ResizeBehavior="BasedOnAlignment"
            Grid.Column="1"
            Grid.Row="0"
            Grid.RowSpan="2"
            Width="8"
            Height="Auto"
            HorizontalAlignment="Left"
            VerticalAlignment="Stretch" 
                Background="AliceBlue"
            Margin="-3 0 0 0">
                <GridSplitter.Style>
                    <Style TargetType="{x:Type GridSplitter}">
                        <Setter Property="Visibility" Value="Hidden"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsMouseOver}">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </GridSplitter.Style>
            </GridSplitter>
Run Code Online (Sandbox Code Playgroud)

你看错了吗?是否GridSplitter必须以不同的方式来称呼?

c# wpf gridsplitter

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

Linq在大名单中

我有两个自定义类Grid,并且Element:

public class Grid
{
    public double ID { get; set; }

    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }

    public MyClass MoreProperties {get; set;}

    public Grid(int id, double x, double y, double z)
    {
        this.ID = id;
        this.X = x;
        this.Y = y;
        this.Z = z;
    }
}
Run Code Online (Sandbox Code Playgroud)

Element:

public abstract class Element
{
    public  int ID { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# linq wpf

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

以相同的方法在新线程中开始

我是线程用法的初学者,在我看过的例子中(这里这里)必须为方法分配一个新线程.但是,有没有办法在方法中制作它?我正在寻找这样的东西:

public void MyMethod()
{
//Start new thread that affects only this method

//Do stuff
//More stuff

}
Run Code Online (Sandbox Code Playgroud)

谢谢.

c# multithreading

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

标签 统计

c# ×10

linq ×3

wpf ×3

count ×2

checkbox ×1

datatemplate ×1

gridsplitter ×1

list ×1

loops ×1

multithreading ×1

mvvm ×1

performance ×1