我有一个自定义BindingList,我想为其创建一个自定义的AddRange方法.
public class MyBindingList<I> : BindingList<I>
{
...
public void AddRange(IEnumerable<I> vals)
{
foreach (I v in vals)
Add(v);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是大型集合的性能很糟糕.我正在调试的情况是尝试添加大约30,000条记录,并花费了不可接受的时间.
在线查看此问题之后,似乎问题在于使用Add每次添加时调整数组大小.我认为这个答案总结为:
如果您使用Add,它会根据需要逐渐调整内部数组的大小(加倍)
我可以在自定义AddRange实现中做什么来指定BindingList需要根据项目数量调整大小,而不是让它在每个项目添加时不断重新分配数组?
我对整个n层体系结构都很陌生,我对使用MVVM和3层应用程序有一些疑问.
根据我的理解,我们有:
我的问题是,如何使用数据访问层将所有这些组合在一起?使用MVVM,我会让模型包含自己加载/更新的方法.相反,这应该是在WCF服务器上发生的事情?如果是这样,是否应将对服务器的引用存储在Model或ViewModel中?它应该怎么称呼?
我有一个带有AutoGenerated列的WPF 4.0 DataGrid.我想只允许用户编辑第一列.有这么简单的方法吗?
我试图添加一个DataGridCell样式并根据ColumnName(第一列总是具有相同的名称)或ColumnIndex设置它的编辑能力,但是我无法为此找出正确的XAML,或者即使它是可能的.
如果我将Column的宽度设置为*,则它们最初的宽度相同,但如果某个项目大于允许的数量,则它将拉伸列宽.
如何通过明确定义大小来强制我的Grid保持其列的大小相同?
我不能使用UniformGrid,因为这个Grid正在ItemsControl中使用,而Items需要放在特定的Grid.Row/ Grid.Columnspot中
编辑这是我当前代码的示例.
<DockPanel>
<!-- Not showing code here for simplicity -->
<local:ColumnHeaderControl DockPanel.Dock="Top" />
<local:RowHeaderControl DockPanel.Dock="Left" />
<ItemsControl ItemsSource="{Binding Events}">
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column"
Value="{Binding DueDate.DayOfWeek,
Converter={StaticResource EnumToIntConverter}}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsPanel>
</ItemsControl>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)
编辑#2这是我的最终解决方案.它使列的大小正确,并在应用程序调整大小时保持大小正确.
<ColumnDefinition Width="{Binding
ElementName=RootControl,
Path=ActualWidth,
Converter={StaticResource MathConverter},
ConverterParameter=(@VALUE-150)/7}" …Run Code Online (Sandbox Code Playgroud) 我有List<T>24小时内的可用时间,以及两个TimeSpans,minTime和maxTime.
我需要找到内一天的时间List<T>,该之间土地minTime和maxTime,但是由于这个正在多个时区中使用时,minTime和MAXTIME可以在不同的日子和跨越像下午1点至凌晨1点,第二天
我最接近的就是这个,但我觉得我在这里缺少一些主要的组件,或者做一些非常低效的事情,因为我对这个TimeSpan对象很新.我只是无法弄清楚是什么......
// Make new TimeSpan out of maxTime to eliminate any extra days (TotalHours >= 24),
// then check if time on the MaxTime is earlier than the MinTime
if (new TimeSpan(maxTime.Hours, maxTime.Minutes, maxTime.Seconds) < minTime)
{
// If time on MaxTime is earlier than MinTime, the two times span separate days,
// so find first time after minTime OR before maxTime
nextAvailableTime = Times.FirstOrDefault(p => …Run Code Online (Sandbox Code Playgroud) 是否有将decimal?数据类型转换为对象的单行方式?
我的代码看起来像这样:
foreach(DataRow row in dt.Rows)
{
var a = new ClassA()
{
PropertyA = row["ValueA"] as decimal?,
PropertyB = row["ValueB"] as decimal?,
PropertyC = row["ValueC"] as decimal?
};
// Do something
}
Run Code Online (Sandbox Code Playgroud)
但是,将对象转换为a decimal?不会按照我期望的方式工作,并且每次都返回null.
从Excel文件中读取数据行,因此每行中对象的数据类型double如果有值则为a,string如果为空则为null .
执行此强制转换的建议方法是使用decimal.TryParse,但是我不想为类上的每个小数属性创建临时变量(我的实际类中有大约7个属性是小数,而不是3).
decimal tmpvalue;
decimal? result = decimal.TryParse((string)value, out tmpvalue) ?
tmpvalue : (decimal?)null;
Run Code Online (Sandbox Code Playgroud)
有没有办法可以将一个潜在的空值转换为decimal?一行?
我尝试了这里发布的答案,但它似乎没有起作用,也给了我一个null价值,因为row["ValueA"] as string回报null.
我有一堆DataTables需要转换为object[,]数组(而不是 object[][]数组).在性能方面,最有效的方法是什么?
我知道我可以通过我的建设做这个object[dt.Rows.Count, dt.Columns.Count]最初,然后通过行循环和解析每一行到阵列中的地方,但我相当肯定还有其他方法,如使用LINQ或System.Data特定的功能,如dataRow.ToItemArray()它可更有效率.
我DataTables是可变大小,除了字符串之外,还包含需要格式化的日期和数字.
例如,如果包含我的一个数据表
Id Name Date Value 1 Rachel 1/1/2013 00:00:00 100.0000 2 Joseph 3/31/2012 00:00:00 50.0000 3 Sarah 2/28/2013 00:00:00 75.5000
那么我想要一个object[,]包含完全相同数据的数组(理想情况下是标题),但是需要格式化的日期和值
arr[x,0] = row[x].Field<int>("Id");
arr[x,1] = row[x].Field<string>("Name");
arr[x,2] = row[x].Field<DateTime>("Date").ToString("M/d/yy");
arr[x,3] = row[x].Field<decimal>("Value").ToString("C2"); // Currency format
Run Code Online (Sandbox Code Playgroud) 我有一个跟随xaml的窗口:
<Window x:Class="TestDemoApp.TreeViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TreeViewWindow" Height="300" Width="300">
<Window.Resources>
<Style TargetType="Control" x:Key="FocusedStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle StrokeThickness="1"
Stroke="Red"
StrokeDashArray="1 2 3 4"
SnapsToDevicePixels="true"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TreeViewItem">
<Setter Property="IsTabStop" Value="True"/>
<Setter Property="Focusable" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusedStyle}"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
</Style>
<Style TargetType="ListViewItem">
<Setter Property="IsTabStop" Value="True"/>
<Setter Property="Focusable" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusedStyle}"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListView TabIndex="1" BorderThickness="5" Focusable="True" IsTabStop="True" KeyboardNavigation.TabNavigation="Continue" FocusVisualStyle="{StaticResource FocusedStyle}">
<ListViewItem TabIndex="2" Content="List Item …Run Code Online (Sandbox Code Playgroud) 我想创建一个最多可以存储一个对象副本的类。此处存储的所有对象将共享相同的基类,我希望能够根据其类型获取对象。
到目前为止,我已经提出了这个解决方案,但是我觉得使用Type作为Dictionary键会出错。
在多个模块中使用的基类
interface ISessionVariables { }
Run Code Online (Sandbox Code Playgroud)
用于访问的普通单例类的示例
public class SessionVariables
{
private object _sync = new object();
private Dictionary<Type, ISessionVariables> _sessionVariables =
new Dictionary<Type, ISessionVariables>;
public T Get<T>()
where T : ISessionVariable, new()
{
lock (_sync)
{
ISessionVariables rtnValue = null;
if (_sessionVariables.TryGetValue(typeof(T), out rtnValue))
return (T)rtnValue;
rtnValue = new T();
_sessionVariables.Add(typeof(T), rtnValue);
return (T)rtnValue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样我可以从各个模块中这样称呼它
SessionVariableSingleton.Get<ModuleASessionVars>().PropertyA;
SessionVariableSingleton.Get<ModuleCSessionVars>().PropertyC;
Run Code Online (Sandbox Code Playgroud)
这是存储这种数据结构的可接受方法吗?还是有一个更好的选择,使用没有类型键的列表或字典?
我们使用a NotifyIcon在用户收到需要他们注意的新消息时提醒用户.如果其他人首先收到消息,则通知图标应该再次隐藏,但是我在查找如何从后面的代码中关闭气球时遇到问题.
我的代码看起来像这样:
myNotifyIcon.ShowBalloonTip(2000, title, message, icon);
Run Code Online (Sandbox Code Playgroud)
我尝试过这里的建议,但没有一个是合适的.
使用myNotifyIcon.Visible = true不会隐藏它
使用myNotifyIcon.Visible = false; myNotifyIcon.Visible = true;将隐藏它,但它也隐藏了托盘中的图标,当它再次显示时,它会显示一个不同的位置.
myNotifyIcon.Show(0) 不是一个有效的方法
myNotifyIcon.ShowBalloonTip(0)或者myNotifyIcon.ShowBalloonTip(1)似乎不起作用,因为气球只是显示出来并且似乎根本不会自行消失.
我看了这个问题,有关使用WinAPI的查找窗口,并发送一个WM_CLOSE消息,但我也不太清楚该怎么做可靠.
如何关闭NotifyIcon后面的代码?