小编Bri*_*ett的帖子

在DataGridCell工具提示中显示验证错误

我有一个WPF DataGrid,它显示实现IDataErrorInfo的类型.正如预期的那样,当验证失败时,行会获得红色感叹号,无效的单元格将获得红色突出显示. 在此输入图像描述

这一切都很好; 但是,我希望验证错误消息显示在无效单元格的工具提示中,以便用户有一些错误的指示.我现在有:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self},
                                                Path=(Validation.Errors[0].ErrorContent}"/>
     </Style>
</DataGrid.CellStyle>
Run Code Online (Sandbox Code Playgroud)

这种方法适用TextBox但不适用DataGridCell.有什么不同?

wpf datagrid

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

在using语句中调用异步方法

我想使用异步调用发送消息,但在此过程中使用了可用资源.我不确定如何正确处理这个问题.

using(IDisposable disposeMe = new DisposableThing())
{
    MethodAsync(disposeMe);
}
Run Code Online (Sandbox Code Playgroud)

任何想法如何做到这一点.异步方法没有任何回调.

编辑:

基于@Servy接受的答案的建议.实施改为:

public async Task Method()
{
    using(IDisposable disposeMe = new DisposableThing())
    {
        await MethodAsync(disposeMe);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asynchronous

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

在DrawingImage中绘制格式化文本

我试图在XAML中包含一些格式化文本作为绘图的一部分.这可能吗?怎么做?

例:

<DrawingImage>
    <DrawingImage.Drawing>
      <!-- Can text be drawn here? -->
    </DrawingImage.Drawing>
</DrawingImage>
Run Code Online (Sandbox Code Playgroud)

wpf drawing

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

如何在ViewModel中公开Model的集合

我正在为表示递归嵌套类型的类实现MVVM.例如:

class NestedType
{
    // Other properties here

    public Collection<NestedType> SubElements {get; set;}
}

class NestedTypeViewModel
{
    ObservableCollection<NestedType> ModelCollection { ??? }
}
Run Code Online (Sandbox Code Playgroud)

如何以可观察的方式公开模型集合中的项目?(即,视图将添加,创建和修改子元素)我假设我需要ViewModel中的集合,ObservableCollection<T>但在模型中呢?我也可以做到这一点ObservableCollection,直接暴露它......

有什么建议?

c# wpf mvvm

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

跨方法边界的LINQ to SQL规则

为了保持代码清洁,我经常尝试将LINQ to SQL中的部分数据访问代码分解为私有子方法,就像使用普通的业务逻辑代码一样.让我举一个非常简单的例子:

public IEnumerable<Item> GetItemsFromRepository()
{
    var setA = from a in this.dataContext.TableA
               where /* criteria */
               select a.Prop;

    return DoSubquery(setA);
}

private IEnumerable<Item> DoSubQuery(IEnumerable<DateTimeOffset> set)
{
     return from item in set
            where /* criteria */
            select item;
}
Run Code Online (Sandbox Code Playgroud)

我敢肯定,通过更深层次的嵌套或使用集合的结果来过滤其他查询,可以想象更复杂的例子.

我的基本问题是:我已经看到了一些重要的性能差异,甚至只是简单地在私有方法中重新组织LINQ to SQL代码而抛出异常.任何人都可以解释这些行为的规则,以便我可以就如何编写高效,干净的数据访问代码做出明智的决定吗?

我有过的一些问题:

1)System.Linq.Table instace传递给某个方法何时导致查询执行?

2)何时在另一个查询中使用System.Linq.Table导致执行?

3)对于System.Linq.Table将参数传递给方法,可以应用哪些类型的操作(Take,First,Last,order by等)是否有限制?

c# linq-to-sql

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

Jasmine单元测试不等待承诺解决

我有一个像这样的异步依赖的角度服务

(function() {
    angular
        .module('app')
        .factory('myService', ['$q', 'asyncService',

    function($q, asyncService) {

        var myData = null;

        return {
            initialize: initialize,
        };

        function initialize(loanId){
            return asyncService.getData(id)
                .then(function(data){
                    console.log("got the data!");
                    myData = data;
            });
        }
    }]);
})();
Run Code Online (Sandbox Code Playgroud)

我想单元测试initialize函数,我正在尝试像这样的茉莉:

describe("Rate Structure Lookup Service", function() {

    var $q;
    var $rootScope;
    var getDataDeferred;
    var mockAsyncService;
    var service;

    beforeEach(function(){
        module('app');

        module(function ($provide) {
            $provide.value('asyncService', mockAsyncService);
        });

        inject(function(_$q_, _$rootScope_, myService) {
            $q = _$q_;
            $rootScope = _$rootScope_;
            service = myService;
        });

        getDataDeferred = $q.defer();

        mockAsyncService …
Run Code Online (Sandbox Code Playgroud)

javascript jasmine angularjs

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

如何设置WPFExtensions.ZoomControl默认缩放

有谁知道如何在wpfextensions中包含的ZoomControl中将默认缩放级别设置为1:1 ?项目首次启动时,默认行为是缩放以填充.

我试过了两个

 <zoom:ZoomControl Mode="Original">
Run Code Online (Sandbox Code Playgroud)

和

 <zoom:ZoomControl Zoom="1">
Run Code Online (Sandbox Code Playgroud)

两者都不起作用......

wpf

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

ControlTemplate中的UserControl

我有一个Telerik Tile的ControlTemplate,我如下所示:

<ControlTemplate TargetType="{x:Type ctrl:Tile}">
    <Border>  

        <local:UserControl>
            <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
        </local:UserControl>
    </Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我的用户控件如下:

    <DockPanel>
        <!-- some content -->

        <ContentPresenter/>

    </DockPanel>
Run Code Online (Sandbox Code Playgroud)

ControlTemplate不显示UserControl的内容.

如果我将控制模板更改为:

<ControlTemplate TargetType="{x:Type ctrl:Tile}">
    <Border>  
        <StackPanel>
            <local:UserControl/>

            <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
       </StackPanel>
    </Border>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

它会找到内容并妥善放置.看起来ControlTemplate一旦嵌入我的UserControl中就找不到内容.有什么我可能做错了吗?

请注意,这些ControlTemplate项目出现在ItemsPresenter中.

wpf telerik controltemplate

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

二进制搜索与LINQ选择语句

我有一个浮点数据列表,我想在其中找到传递值下方的索引.一个简化的例子:

double[] x= {1.0, 1.4, 2.3, 5.6, 7.8};
double[] y= {3.4, 8.2, 5.3, 8.1, 0.5};
int lowerIndex = BinaryIndexSearch(x, 2.0);  // should return 1
Run Code Online (Sandbox Code Playgroud)

这样做的目的是插值将然后执行x并y使用lowerIndex和lowerIndex+1.

二进制索引搜索算法看起来像

    int BinaryIndexSearch(double[] x, double value)
    {
        int upper = x.Length - 1;
        int lower = 0;
        int pivot;

        do
        {
            pivot = (upper + lower) / 2;

            if (value >= x[pivot])
            {
                lower = pivot + 1;
            }
            else
            {
                upper = pivot - 1;
            } …
Run Code Online (Sandbox Code Playgroud)

c# linq

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

使用LINQ查询初始化List <int>

我List<int>通过存储两个简单常量MinValue和MaxValue来初始化构造函数:

private const int MinValue = 1;
private const int MaxValue = 100;

private List<int> integerList = new List<int>();

public Class()
{
    for (int i = MinValue ; i < MaxValue ; i++)
    {
        integerList .Add(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法用简单的LINQ查询初始化列表?由于a List<T>可以用a构造IEnumerable<T>,是否存在以下形式的查询?

private List<int> integerList = new List<int>(<insert query here>);
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?

c# linq

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