我有这个扩展标记
public class NullableExtension : TypeExtension
{
public NullableExtension() {
}
public NullableExtension( string type )
: base(type) {
}
public NullableExtension( Type type )
: base(type) {
}
public override object ProvideValue( IServiceProvider serviceProvider ) {
Type basis = (Type)base.ProvideValue( serviceProvider );
return typeof(Nullable<>).MakeGenericType( basis );
}
}
Run Code Online (Sandbox Code Playgroud)
它旨在提供其他类型的可空版本.当在"普通"XAML中使用时,它按预期工作.例如,如
<SomeControl DataContext="{My:Nullable System:Int32}"/>
Run Code Online (Sandbox Code Playgroud)
(假设My是为保存扩展的C#名称空间定义的XML名称空间,对于System来说类似).控件的数据上下文设置为System.Type
for,Nullable<int>
如我所料.
但是,当我使用此扩展来尝试设置类似的DataType
属性时DataTemplate
<DataTemplate DataType="{My:Nullable System:Int32}">
<TextBlock ... />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
编译器告诉我的是
字典的键不能是'System.Windows.Controls.Primitives.TextBlock'类型.只支持String,TypeExtension和StaticExtension."
和
"类型'NullableExtension'的构造函数没有1个参数.
有人知道为什么只允许这三种方法(甚至不是TypeExtension
像我的子类一样)?那时处理XAML有什么特别之处?是否有另一种方法来实现这一点(基于可以为空的类型的数据模板选择)而不诉诸DataTemplateSelector
?
我有DataGrid
一个Grid
布局容器中的控件,我似乎无法自动滚动DataGrid
自己工作.我可以包围DataGrid
a ScrollViewer
并因此添加滚动条,但自动滚动不起作用.
所以现在,当添加新条目时DataGrid
,DataGrid
只是垂直扩展.我想启用垂直滚动条允许滚动到DataGrid
添加更多项目时的项目,而不是原始垂直大小可以显示,而不是整个展开DataGrid
.当然,必须有一种简单的方法来实现这一目标.
我正在学习这门SynchronizationContext
课程.我试图了解SynchronizationContext.SetSynchronizationContext()
在WinForm/WPF应用程序的上下文中调用的常见用法场景是什么.设置SynchronizationContext
线程是什么意思?我什么时候该做,为什么?另外,如果我设置它,我应该在某个时候取消它吗?
编辑:
在他的回答中,@ Hans Passant问我为什么在考虑SetSynchronizationContext()
.我的想法是在工作线程上设置上下文,以便在该线程上运行的代码将具有要使用的上下文.
private void button3_Click(object sender, EventArgs e)
{
var syncContext = SynchronizationContext.Current;
Task.Factory.StartNew(() =>
{
// Setup the SynchronizationContext on this thread so
// that SomeAsyncComponentThatNeedsACurrentContext
// will have a context when it needs one
if (SynchronizationContext.Current == null)
SynchronizationContext.SetSynchronizationContext(syncContext);
var c = new SomeAsyncComponentThatNeedsACurrentContext();
c.DoSomething();
});
}
Run Code Online (Sandbox Code Playgroud) 我DataTable
在我的应用程序中使用了一些计算.我需要通过除第一行之外的所有行进行迭代.可能吗?
就像是:
DataTable dt;
foreach (DataRow r in dt.Rows /*EXCEPT THE FIRST ONE*/)
{
//do something...
}
Run Code Online (Sandbox Code Playgroud) 我需要能够在T4模板中使用一个类,这两个类都驻留在我的解决方案中.我有一个名为类Container
在Container.cs
.在我的文本模板中,我希望能够访问Container
类.关于我该怎么做的任何想法.基本上是这样的:
<#@ template language="C#" #>
<# var container = new Container() #>
有没有办法从WPF中的WinForms完成此功能?
ListView.FocusedItem = ListView.Items[itemToFocusIndex]
Run Code Online (Sandbox Code Playgroud)
我正在尝试在WPF ListView中的项目上手动设置焦点(而不是选择).来自System.Windows.Controls.谢谢.
在将其出售给客户之前,我制作了一个我需要许可的程序.由于我不确定,它至少会获得商业许可软件的成本,我不想买一个.
所以我正在寻找有关如何使用以下逻辑实现许可功能的建议:
我不知道的是如何保护我的程序免受简单的反编译/反汇编,只是删除一个检查许可证的电话?或者制作自己的服务器,以便在每个许可证上做出响应?这种安全性通常如何?
我搜索了免费的SDK或类似的东西,但找不到任何东西.我非常感谢你的帮助!
PS:我并不希望让这个系统"坚不可摧",但我希望得到的保护措施要比支付5.10美元的程序副本更加昂贵.
PPS:抱歉我的英语不好.
原则上,我开发了一种将RadioButtons绑定到几乎任何东西的简洁方法:
/// <summary>Converts an value to 'true' if it matches the 'To' property.</summary>
/// <example>
/// <RadioButton IsChecked="{Binding VersionString, Converter={local:TrueWhenEqual To='1.0'}}"/>
/// </example>
public class TrueWhenEqual : MarkupExtension, IValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object To { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return object.Equals(value, To);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value) return To;
throw new NotSupportedException();
} …
Run Code Online (Sandbox Code Playgroud) 将异步调用转换为同步是否有任何良好的实践(模式)?
我有一个第三方库,其方法都是异步的,以获得任何方法的结果,你必须听一个事件,这将带来一些上下文.基本上它看起来像:
service.BeginSomething(...);
service.OnBeginSomethingCompleted += ;
Run Code Online (Sandbox Code Playgroud)
我需要的是在BeginSomething完成后执行一些代码(因此在触发OnBeginSomethingCompleted之后).在事件中处理响应非常不方便.
我能想到的唯一方法是运行Thread.Sleep循环并等待表单上的某个字段更新,但它看起来不是非常优雅的sollution.
我正在使用.net 4.0.
我为我的SSRS项目创建了一个自定义程序集.
Custom Assembly有2个功能,IsInGroup
并且MyTest
:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
namespace SSRS_Custom_Fuctions
{
public class Class1
{
public static bool IsInGroup(string user, string group)
{
using (var identity = new WindowsIdentity(user))
{
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(group);
}
}
public static string MyTest()
{
return "Hello World";
}
}
}
Run Code Online (Sandbox Code Playgroud)
1)MyTest
返回字符串'Hello World' 的基本函数在使用表达式的Report中完全正常工作=SSRS_Custom_Functions.Class1.MyTest()
2)IsInGroup
返回布尔值的函数不起作用.这是使用System.Security.Principal
命名空间来检查传递给函数的用户名是否存在于传递给函数的组中.尝试使用表达式调用它时=SSRS_Custom_Functions.Class1.IsInGroup(User.User1, "MyGroupName")
,报告正在退出,并显示以下错误消息:
请求System.Security类型的权限失败
我已根据Microsoft KB920769修改了rssrvpolicy.config
ReportingServices文件路径和RSPreviewPolicy.config
VisualStudio文件路径中的配置文件. …
c# ×7
.net ×5
wpf ×5
xaml ×2
.net-4.0 ×1
asynchronous ×1
data-binding ×1
datagrid ×1
datarow ×1
datatable ×1
datatemplate ×1
focus ×1
iteration ×1
licensing ×1
radio-button ×1
scroll ×1
scrollbar ×1
ssrs-2008 ×1
t4 ×1
winforms ×1
wpf-controls ×1