小编And*_*rke的帖子

c#Reflection - 查找集合的通用类型

我正在反映一个属性'Blah',它的Type是ICollection

    public ICollection<string> Blah { get; set; }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var pi = GetType().GetProperty("Blah");
        MessageBox.Show(pi.PropertyType.ToString());
    }
Run Code Online (Sandbox Code Playgroud)

这给了我(正如你所期待的那样!)ICollection<string>......

但我真的想要收集类型即ICollection(而不是ICollection<string>) - 有谁知道我该怎么做呢?

c# reflection types properties

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

wpf错误模板 - 在扩展器崩溃时仍然可以看到红色框

我正在对ExpBox中的TextBox的DataSource进行一些验证,并且发现一旦触发了验证错误,如果我折叠了Expander,红色框就会停留在TextBox所在的位置.

<Expander Header="Blah Blah Blah">
  <TextBox Name="TextBox"
           Validation.ErrorTemplate="{DynamicResource TextBoxErrorTemplate}"
           Text="{Binding Path=Blah,
                          UpdateSourceTrigger=PropertyChanged,
                          ValidatesOnDataErrors=True}" />
</Expander>
Run Code Online (Sandbox Code Playgroud)

我试图通过将错误模板的可见性绑定到扩展器来解决这个问题,但是我认为绑定有问题.

<local:NotVisibleConverter x:Key="NotVisibleConverter" />

<ControlTemplate x:Key="TextBoxErrorTemplate">
  <DockPanel>
    <Border BorderBrush="Red" BorderThickness="2" 
            Visibility="{Binding Path=IsExpanded, 
                                 Converter={StaticResource NotVisibleConverter}, 
                                 RelativeSource={RelativeSource AncestorType=Expander}}" >
      <AdornedElementPlaceholder Name="MyAdorner" />
    </Border>
  </DockPanel>
  <ControlTemplate.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self}, 
                                Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我想我的绑定出了问题,有人可以让我回到正轨吗?或者,是否有人知道ErrorTemplate的另一个解决方案仍然可以在Expander崩溃时看到?

validation wpf binding controltemplate

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

wpf usercontrol,将按钮的命令参数绑定到父usercontrol

我有一个包含删除按钮的WPF UserControl,我想将整个UserControl作为CommandParameter传递.

目前绑定设置为CommandParameter ="{Binding RelativeSource = {RelativeSource Self}}",它给了我按钮,但是如何获得整个控件?

有人可以帮忙吗?

干杯,

安迪

<UserControl x:Class="GTS.GRS.N3.Controls.LabelledTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="155" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Label Name="label" HorizontalAlignment="Left" Width="96">Label</Label>
    <TextBox Name="textBox" Grid.Column="1" />
    <Button Grid.Column="2" Style="{DynamicResource CloseButton}" Command="{Binding RemoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" Visibility="{Binding RemoveVisible}"></Button>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

wpf binding user-controls

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

使用Async和Await分解数据库调用(使用Dapper)

我们从Dapper请求数千个对象并达到参数限制(2100),因此决定以块的形式加载它们.

我认为这将是一个尝试异步等待的好机会 - 这是我第一次走了,所以也许会让学校男生出错!

断点正在受到打击,但整个事情都没有回归.这不是一个错误 - 它似乎只是一切都在黑洞!

请帮忙!

这是我原来的方法 - 它现在调用Async方法

    public List<MyObject> Get(IEnumerable<int> ids)
    {
        return this.GetMyObjectsAsync(ids).Result.ToList();
    }  //Breakpoint on this final bracket never gets hit
Run Code Online (Sandbox Code Playgroud)

我添加了此方法将id拆分为1000块,然后等待任务完成

    private async Task<List<MyObject>> GetMyObjectsAsync(IEnumerable<int> ids)
    {
        var subSets = this.Partition(ids, 1000);

        var tasks = subSets.Select(set => GetMyObjectsTask(set.ToArray()));

        //breakpoint on the line below gets hit ...
        var multiLists = await Task.WhenAll(tasks);

        //breakpoint on line below never gets hit ...
        var list = new List<MyObject>();
        foreach (var myobj in multiLists)
        {
            list.AddRange(myobj);   
        }
        return …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous async-await dapper

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

在实体框架4.1(CodeFirst)中更新对子对象的引用

我正在尝试使用EntityFramework 4.1(CodeFirst)更新我之前保存的对象

Job类具有以下属性......

public class Job
{
    [key]
    public int Id { get; set; }
    public string Title { get; set; }
    public Project Project { get; set; }
    public JobType JobType { get; set; }
    public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

初始创建工作正常,但更新只提交对字符串的更改.

如果我改变的子对象,例如在JobType从地产JobTypeAJobTypeB-更改不会犯...

我不打算对JobType进行更改 - 仅限于Job.

using (var context = new JobContext())
{
    context.Jobs.Attach(job);
    context.Entry(job).State = EntityState.Modified;
    context.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)

看看SQL Profiler - 甚至没有为更新发送ID - 但它们是用于初始插入!

c# entity-framework code-first ef-code-first

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

Fluent断言:在DateTime属性的集合上使用BeCloseTo

我正在处理许多项目,每个项目都包含一个DateProcessed属性(可以为空的DateTime),并希望Assert将该属性设置为当前日期.当它完成处理程序时,日期都略有不同.

我想测试所有DateProcessed属性具有相对性(100ms)最近的DateTime.

Fluent Assertions具有.BeCloseTo方法,适用于单个项目.但我想将它用于整个系列.但是在查看集合时,它不能通过Contains()获得.

一个简化的例子......

[TestFixture]
public class when_I_process_the_items
{
    [SetUp]
    public void context()
    {
        items = new List<DateTime?>(new [] { (DateTime?)DateTime.Now, DateTime.Now, DateTime.Now } );
    }

    public List<DateTime?> items;

    [Test]
    public void then_first_item_must_be_set_to_the_current_time()
    {
        items.First().Should().BeCloseTo(DateTime.Now, precision: 100);
    }

    [Test]
    public void then_all_items_must_be_set_to_the_current_time()
    {
        items.Should().Contain .... //Not sure? :(
    }

}
Run Code Online (Sandbox Code Playgroud)

nunit unit-testing fluent-assertions

8
推荐指数
2
解决办法
3704
查看次数

Unity容器 - 动态传递给Resolve方法

我创建了一个我输入的ISearchable接口,以便我可以检索结果的IEnumerable.

我有许多服务为不同的域对象实现ISearchable ...

Container.RegisterType<ISearchable<Animal>, AnimalService>();
Container.RegisterType<ISearchable<Fish>, FishService>();
Run Code Online (Sandbox Code Playgroud)

我想根据类型解决(通过Unity)一个ISearchable,但我正努力让它工作......

以下dos不能编译,但希望能够了解我正在努力实现的目标.

Type t = typeof(Animal);
var searchProvider = _container.Resolve<ISearchable<t>>();
Run Code Online (Sandbox Code Playgroud)

任何帮助感激地收到!

谢谢,

安迪

c# ioc-container inversion-of-control unity-container

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

使用MVVM的WPF ReadOnly依赖项属性

我最近重写了DevXpress WPF网格,给自己一个SelectedObject属性,我可以从松散绑定的ViewModel访问它.

我已经创建了一个SelectedObject依赖项属性,并在我的XAML中绑定了OneWayToSource.

Everthing工作正常,但如果我尝试将其设为ReadOnly(为了完整性),我会收到编译错误并说我无法绑定到ReadOnly属性.下面的代码编译,我已经包括(但重新列出)我尝试获取属性ReadOnly时尝试的位.

有人可以帮忙吗?

我重写的控件的依赖属性如下所示:

  //public static readonly DependencyPropertyKey SelectedRowKey = DependencyProperty.RegisterReadOnly("SelectedObject", typeof(object), typeof(MyGrid), new PropertyMetadata(null));
//public static readonly DependencyProperty SelectedObjectProperty = SelectedRowKey.DependencyProperty;

public readonly static DependencyProperty SelectedObjectProperty = DependencyProperty.Register("SelectedObject", typeof(object), typeof(MyGrid), new PropertyMetadata(null));

public object SelectedObject
{
    get
    {

        return GetValue(SelectedObjectProperty);
    }
    set
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML是:

 <StackPanel>
  <devxgrid:MyGrid AutoPopulateColumns="True" DataSource="{Binding Animals}" SelectedObject="{Binding MyObject, Mode=OneWayToSource}" Width="300" Height="300">
    <devxgrid:MyGrid.View>
        <MyGrid:TableView AllowEditing="False" Name="GridView" AutoWidth="True" />
    </devxgrid:MyGrid.View>
 </devxgrid:MyGrid>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

wpf binding dependency-properties mvvm

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

WPF单选按钮 - MVVM - 绑定似乎死了?

DataContext将以下Window 绑定到后面的代码,给我一个MVVM style来演示这种行为:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <StackPanel>
        <RadioButton GroupName="test" Content="Monkey" IsChecked="{Binding IsMonkey}"/>
        <RadioButton GroupName="test" Content="Turtle" IsChecked="{Binding IsTurtle}" />
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

下面是代码背后的代码:

public partial class Window1
{
    public Window1()
    {
        InitializeComponent();
    }

    private bool _isMonkey;
    public bool IsMonkey
    {
        get { return _isMonkey; }
        set
        {
            _isMonkey = value;
        }
    }

    private bool _isTurtle;
    public bool IsTurtle
    {
        get { return _isTurtle; }
        set
        {
            _isTurtle = value;
        }
    }
} …
Run Code Online (Sandbox Code Playgroud)

.net wpf binding mvvm radio-button

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

WPF ItemsControl - ViewModel上的命令未从ItemsControl中触发

我正在使用MV-VM并在我的ViewModel上有一个名为'EntitySelectedCommand'的命令.

我试图获取ItemsControl中的所有项目以触发此命令,但它无法正常工作.

我认为这是因为每个项目的"datacontext"是项目绑定的单个对象,而不是ViewModel?

有人能指出我正确的方向吗?

干杯,

安迪

<ItemsControl  ItemsSource="{Binding Path=LinkedSuppliers}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Controls:EntityLabel Grid.Column="0" Grid.Row="0" Content="{Binding Name}" CurrentEntity="{Binding }" EntitySelected="{Binding EntitySelectedCommand}" ></Controls:EntityLabel>                
            <StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

data-binding wpf itemscontrol mvvm

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