小编bao*_*ozi的帖子

如果值为负,WPF DataGridColumn 前景色变为红色

我希望 a 的前景色DataGridColumn根据其值改变。我有

<DataGridTextColumn x:Name="Diff1" 
                    Binding="{Binding Change}" Header="Net Chng" 
                    Width="*" IsReadOnly="True"
                    Foreground="{Binding Change,Converter={StaticResource negativeToColor}}">
</DataGridTextColumn>
Run Code Online (Sandbox Code Playgroud)

和转换器

public class negativeToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        SolidColorBrush brush = new SolidColorBrush(Colors.LimeGreen);
        double doubleValue = 0.0;
        Double.TryParse(value.ToString(), out doubleValue);

        if (doubleValue < 0)
            brush = new SolidColorBrush(Colors.Red);

        return brush;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

但转换器显示没有效果。

在此处输入图片说明

c# wpf

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

如何使用c ++生成不相关的随机序列

我想生成两个不相关的正态分布随机数X 1,X 2的序列.

由于正态分布随机数来自统一数,我只需要两个不相关的统一序列.但是如何使用:

srand (time(NULL));
Run Code Online (Sandbox Code Playgroud)

我想我需要种子两次或做类似的事情?

c++ random math

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

为什么TPL Dataflow阻塞.LinkTo不提供任何输出?

我对TPL Dataflow这个话题很陌生.在C#中的Concurrency一书中,我测试了以下示例.我无法弄清楚为什么没有输出应该是2*2-2=2;

static void Main(string[] args)
{
    //Task tt = test();
    Task tt = test1(); 
    Console.ReadLine();
}
static async Task test1()
{
    try
    {
        var multiplyBlock = new TransformBlock<int, int>(item =>
        {
            if (item == 1)
                throw new InvalidOperationException("Blech.");
            return item * 2;
        });
        var subtractBlock = new TransformBlock<int, int>(item => item - 2);
        multiplyBlock.LinkTo(subtractBlock,
        new DataflowLinkOptions { PropagateCompletion = true });
        multiplyBlock.Post(2);
        await subtractBlock.Completion;
        int temp = subtractBlock.Receive();
        Console.WriteLine(temp);
    }
    catch (AggregateException e) …
Run Code Online (Sandbox Code Playgroud)

c# tpl-dataflow

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

在C#中只写访问器

例如,所谓的只写访问器如何工作 Discount

class CableBill
{
    private int rentalFee;
    private int payPerViewDiscount;
    private bool discount;

    public CableBill(int rentalFee)
    {
        this.rentalFee = rentalFee;
        discount = false;
    }

    public bool Discount
    {
        set
        {
            discount = value;
            if (discount)
                payPerViewDiscount = 2;
            else
                payPerViewDiscount = 0;
        }
    }

    public int CalculateAmount(int payPerViewMoviesOrdered)
    {
        return (rentalFee - payPerViewDiscount) * payPerViewMoviesOrdered;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我写作

CableBill january = new CableBill(4);
MessageBox.Show(january.CalculateAmount(7).ToString());
Run Code Online (Sandbox Code Playgroud)

返回值是28

我的问题是:
程序是如何知道的payPerViewDiscount=0Discount我初始化对象时从未使用过该属性

c#

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

如何使用 Observable.Generate 在控制台中输出

为什么下面的代码没有输出?

它是一个控制台应用程序,用于生成一些随机值(我正在学习反应式扩展)。

using System.Reactive.Linq;

static void Main(string[] args)
{   
    var rand = new Random();
    Observable.Generate(
        5.0,
        i => i > 0,
        i => i + rand.NextDouble() - 0.5,
        i => i,
        i => TimeSpan.FromSeconds(0.1)
    ).Subscribe(Console.WriteLine);  
}
Run Code Online (Sandbox Code Playgroud)

c# system.reactive

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

使用Dispatcher.BeginInvoke()时如何避免竞争条件?

下面的代码只记录a和b之间的素数.c#async await适用于我的代码但旧dispatcher方法给出了奇怪的结果.当我点击按钮时,我得到以下结果:
704,500素数在2000000和2999999
之间67883素数在3000000和3999999
之间66330素数在4000000和4999999
之间65367素数在5000000和5999999之间
这是错的i should be <5 and begin with 1000000.有人帮忙解释这里的竞争状况吗?

private void _button_Click(object sender, RoutedEventArgs e)
{
    //Go();
    Task.Run(() => Go1());
}
void Go1()
{
    Dispatcher.BeginInvoke(new Action(() => _button.IsEnabled = false));
    for (int i = 1; i < 5; i++)
    {
        int result = GetPrimesCount(i * 1000000, 1000000);
        Dispatcher.BeginInvoke(new Action(() =>
        _results.Text += result + " primes between " + (i * 1000000) +
        " and " + ((i + …
Run Code Online (Sandbox Code Playgroud)

c# async-await

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

为什么Parallel.ForEach比顺序循环慢两倍

indexed parallel.foreach与正常的顺序循环相比,为什么以下几乎是两倍慢?它与资源争用有关吗?

static void Main(string[] args)
{
    var values = Enumerable.Range(0, 100000).Select(v => v+v);
    Stopwatch s = Stopwatch.StartNew();
    //int index = 0;

    //foreach (double value in values)
    //{
    //    Console.WriteLine("{0}:\t{1}", index, value);
    //    index++;
    //}
    Parallel.ForEach(values, (value, pls, index) =>
    {
        Console.WriteLine("{0}:\t{1}", index, value);
        index++;
    });
    Console.WriteLine(s.Elapsed.TotalMilliseconds);
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

c# parallel-processing

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

如何在c#中使用静态方法

为什么以下代码返回1,1,1而不是1,2,3?我想保留int temp价值,以便我可以在其他地方使用它.如果我Console.WriteLine(count())直接打电话,它会工作.

class Program
{
    private static int start = 0;
    static void Main(string[] args)
    {
        int temp = count();
        Console.WriteLine(temp);
        temp = count();
        Console.WriteLine(temp);
        temp = count();
        Console.WriteLine(temp);
    }

    static int count()
    {
        return start + 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# static

-3
推荐指数
1
解决办法
91
查看次数