小编Kri*_*rip的帖子

在C#中生成随机浮点数的最佳方法

在C#中生成随机浮点数的最佳方法是什么?

更新:我想从float.Minvalue到float.Maxvalue的随机浮点数.我在一些数学方法的单元测试中使用这些数字.

c# random floating-point

51
推荐指数
3
解决办法
9万
查看次数

访问另一个项目的设置文件

有没有办法从不同的项目访问设置文件?例如,我有一个包含2个项目的解决方案(让我们称之为Proj1和Proj2).我想从Proj1中的Program.cs访问Proj2的应用程序设置.这可能吗?

c# visual-studio-2005 application-settings

42
推荐指数
3
解决办法
4万
查看次数

将System.Array转换为string []

我有一个System.Array,我需要转换为字符串[].有没有更好的方法来做到这一点,而不仅仅是循环遍历数组,在每个元素上调用ToString,并保存到字符串[]?问题是我不一定知道元素的类型,直到运行时.

.net c# arrays system.array

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

更改TFS工作项类型

是否可以更改TFS工作项的类型?例如,我有一个Bug我想更改为变更请求.

tfs visual-studio-2008 tfs-workitem

19
推荐指数
2
解决办法
6734
查看次数

将Float显示为具有至少1个小数位的字符串

我想将float显示为字符串,同时确保显示至少一个小数位.如果有更多的小数,我希望那些显示.

例如:1应显示为1.0 1.2345应显示为1.2345

有人可以帮我格式化字符串吗?

c# string string-formatting

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

从Excel粘贴到WPF DataGrid

我有一个DataGrid(称为TheGrid),我想在其上实现复制和粘贴功能.复制功能很好但我不知道如何实现粘贴.我只需要从剪贴板中获取数据并自己解析吗?

命令绑定:

<Window.CommandBindings>
    <CommandBinding Command="Copy" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute" />
    <CommandBinding Command="Paste" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute" />
</Window.CommandBindings>
Run Code Online (Sandbox Code Playgroud)

菜单项:

<MenuItem Header="{x:Static culture:TextResource.CopyMenuItem}" Command="Copy"/>
<MenuItem Header="{x:Static culture:TextResource.PasteMenuItem}" Command="Paste"/>
Run Code Online (Sandbox Code Playgroud)

CommandBinding_Executed背后的代码:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if(e.Command.Equals(ApplicationCommands.Copy))
    {
        // This works great, wow that was easy!
        ApplicationCommands.Copy.Execute(null, TheGrid);
    }
    else if (e.Command.Equals(ApplicationCommands.Paste))
    {
        //What do I do here? Is there an easy way to paste like there was for copy?
        // Or do I need to grab data using Clipboard.GetData and parse it myself?
    } …
Run Code Online (Sandbox Code Playgroud)

c# wpf clipboard datagrid paste

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

WF和WPF交互的真实世界示例

我正在寻找Windows Presentation Foundation和Workflow Foundation之间交互的一些很好的实际例子.我看到的大多数WF教程都在控制台应用程序中使用.我对使用丰富的WPF接口和WF的应用程序更感兴趣.特别是如果它们允许用户定义的工作流程(允许用户动态设计和运行他们自己的工作流程).

c# wpf workflow-foundation

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

C#使用泛型类而不指定类型

我有一个我创建的泛型类:

public abstract class MyClass<T>
{
    public T Model
    {
        get;
        protected set;
    }        
}
Run Code Online (Sandbox Code Playgroud)

在我的代码中的某些时候,我想用MyClass类型做任何事情.就像是:

private void MyMethod(object param)
{
    myClassVar = param as MyClass;
    param.Model....etc
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?或者我是否需要使MyClass成为某个子类(MyClassBase)或实现接口(IMyClass)?

c# generics

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

ItemsControl拖放

我有一个带有DataTemplate的ItemsControl,它绑定到整数的ObservableCollection.

<ItemsControl Name="DimsContainer" ItemTemplate="{StaticResource DimensionsTemplate}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

并在Windows资源中:

<Window.Resources>
    <DataTemplate x:Key="DimensionsTemplate" >
        <TextBlock Text="{Binding}"
                       Padding="5"
                       VerticalAlignment="Center"
                       FontSize="32"/>
    </DataTemplate>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

我正在尝试实现在ItemsControl中拖放项目的能力(即能够重新排序整数).有没有人有一个如何做到这一点的简单例子?我连接了PreviewMouseMove,DragEnter和Drop事件.问题是我无法弄清楚如何确定拖动哪个项目以及拖动它的位置.似乎整个ItemsControl都被传递到事件中.

c# wpf drag-and-drop itemscontrol

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

XAML中的命令绑定与ViewModel中的ICommand属性

我刚开始在应用程序中使用MVVM命令.我找到了很多例子,并在我的代码中尝试了两种方式.一些示例在xaml中具有命令绑定,如下所示:

<CommandBinding Command="local:MainWindow.OpenRecentFile" 
                Executed="{Binding OpenRecentFile_Executed}" />
...
<MenuItem Header="{x:Static culture:TextResource.RecentFilesMenuItem}" 
          Command="local:MainWindow.RecentFilesCommand" >
Run Code Online (Sandbox Code Playgroud)

使用OpenRecentFile_Executed是ViewModel中的方法和静态ICommand,如下所示:

 public static readonly ICommand OpenRecentFile = 
     new RoutedCommand("Open Recent", typeof(MainWindow));
Run Code Online (Sandbox Code Playgroud)

我还看到了ViewModel上有一个属性ICommand的属性,它在视图中被绑定到这样:

<MenuItem Header="Close Current File" 
          Command="{Binding CloseCurrentFileCommand}" 
          CommandParameter="{TemplateBinding DataContext}"/>
Run Code Online (Sandbox Code Playgroud)

并在ViewModel中:

private ICommand closeCurrentFileCommand;
public ICommand CloseCurrentFileCommand
{
    get
    {
        if (closeCurrentFileCommand == null)
        {
            closeCurrentFileCommand = 
                new RelayCommand(param => this.CloseCurrentCedarFile(param));
        }
        return closeCurrentFileCommand;
    }
}
Run Code Online (Sandbox Code Playgroud)

每种方法有哪些好处/缺点?

c# wpf command

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