我正在反映一个属性'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>) - 有谁知道我该怎么做呢?
我正在对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崩溃时看到?
我有一个包含删除按钮的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) 我们从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) 我正在尝试使用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从地产JobTypeA到JobTypeB-更改不会犯...
我不打算对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 - 但它们是用于初始插入!
我正在处理许多项目,每个项目都包含一个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) 我创建了一个我输入的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)
任何帮助感激地收到!
谢谢,
安迪
我最近重写了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) 我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) 我正在使用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) wpf ×5
binding ×4
c# ×4
mvvm ×3
.net ×1
async-await ×1
asynchronous ×1
code-first ×1
dapper ×1
data-binding ×1
itemscontrol ×1
nunit ×1
properties ×1
radio-button ×1
reflection ×1
types ×1
unit-testing ×1
validation ×1