我希望改变控件的样式,但我基本上想要复制默认样式的一部分.有谁知道我怎么能弄清楚控件的默认样式是什么?
在我的情况下,我想让一个DataGrid中的列标题在鼠标上变成蓝色,就像行标题一样.
我正在编写一些单元测试,我需要能够访问外部文件.我假设我可以将文件放在我的解决方案中,将其标记为复制到输出目录,然后通过相对路径访问它.不幸的是,似乎单元测试是在一个奇怪的目录中运行的.
所以,而不是从:
[MyUnitTestProjectFolder]\bin\Release
Run Code Online (Sandbox Code Playgroud)
它来自:
[MySolution]\\[TheProjectI'mTesting]\TestResults\\[MyUsername]_[MyComputerName] [DateTimeStamp]\Out
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何设置我需要从单元测试中使用的外部文件的访问权限?
请注意,文件不是文本文件.它们是专有的平面文件数据库格式(从另一个应用程序创建),因此在测试运行期间"即时"准备这些文件是不可行的.
我在代码库中有一些依赖于Application.Current.Dispatcher.Invoke ...的方法来确保在GUI线程上运行.我目前正在尝试为这些方法编写单元测试但是(正如预期的那样)Application.Current为null所以我得到一个NullReferenceException.
我试图在他们自己的AppDomain中运行受影响的测试,如下所示:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/786d5c06-0511-41c0-a6a2-5c4e44f8ffb6/
但是当我这样做时,Application.Current仍为null.不应该启动AppDomain为我设置Application.Current?为什么它仍然是空的?
我的代码:基类:
[TestClass()]
[Serializable]
public class UnitTest
{
protected void ExecuteInSeparateAppDomain(string methodName)
{
AppDomainSetup appDomainSetup = new AppDomainSetup();
appDomainSetup.ApplicationBase = Environment.CurrentDirectory;
AppDomain appDomain = AppDomain.CreateDomain(methodName, null, appDomainSetup);
try
{
appDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e)
{
throw e.ExceptionObject as Exception;
};
UnitTest unitTest = appDomain.CreateInstanceAndUnwrap(GetType().Assembly.GetName().Name, GetType().FullName) as UnitTest;
MethodInfo methodInfo = unitTest.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
if (methodInfo == null)
{
throw new InvalidOperationException(string.Format("Method '{0}' not found on type '{1}'.", methodName, unitTest.GetType().FullName));
}
try
{ …Run Code Online (Sandbox Code Playgroud) 我最近问了一个关于如何使用Drag and Drop重新排序ItemsControl的问题(ItemsControl拖放).答案目前很有用(下面的代码),但现在我正在尝试实现MVVM,当前的解决方案需要访问视图中的项目.任何想法如何改变这与MVVM一起工作?我计划将附加属性绑定到命令,但我不知道如何摆脱诸如以下的行:int index = (int)(e.GetPosition(DimsContainer).X / width);
当前的拖放代码:
private void DimsContainer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_isDown = true;
_startPoint = e.GetPosition(this.DimsContainer);
}
private void DimsContainer_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_isDown = false;
_isDragging = false;
if (_realDragSource != null)
{
_realDragSource.ReleaseMouseCapture();
}
}
private void DimsContainer_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (_isDown)
{
if ((_isDragging == false) &&
((Math.Abs(e.GetPosition(this.DimsContainer).X - _startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance) ||
(Math.Abs(e.GetPosition(this.DimsContainer).Y - _startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance)))
{
_isDragging = true;
_realDragSource = e.Source …Run Code Online (Sandbox Code Playgroud) Style当属性和数据值为true时,我需要能够更改控件.例如,我的绑定数据有一个IsDirty属性.如果IsDirty为true,我想更改控件的背景颜色并选择控件.我找到了MultiTrigger和MultiDataTrigger类......但在这种情况下,我需要以某种方式触发数据和属性.我怎样才能做到这一点?
另一个注意事项:我需要能够在XAML背后的代码中执行此操作.
我想根据另一个ComboBox中是否选择了一个项来启用/禁用ComboBox.我能够通过在Style上设置触发器来使其工作,但这会覆盖我对组合框的自定义全局样式.有没有另一种方法来获得相同的功能而不会失去我的风格?
<ComboBox Grid.Column="1" Grid.Row="1"
Name="AnalysisComboBox"
MinWidth="200"
VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding Path=AvailableAnalysis}">
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEnabled" Value="True" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem,ElementName=ApplicationComboBox}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
Run Code Online (Sandbox Code Playgroud) 我在单元格上设置了验证,它按预期工作(在文本框周围放置一个红色突出显示,并添加错误的工具提示).但是,如果我尝试访问Valgen.GetHasError(TheGrid),其中TheGrid是我的DataGrid,它始终为false.有谁知道如何检查DataGrid中的任何单元格是否有错误?
我想这样做,所以如果有错误我可以禁用保存.
我的应用程序中有以下代码:
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
int x, int y, int width, int height, uint flags);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, uint msg,
IntPtr wParam, IntPtr lParam);
Run Code Online (Sandbox Code Playgroud)
我从Code Analysis(FxCop)收到以下警告:
CA1060:Microsoft.Design:因为它是P/Invoke方法,所以应该在名为NativeMethods,SafeNativeMethods或UnsafeNativeMethods的类中定义'IconHelper.GetWindowLong(IntPtr,int)'.
有人可以告诉我应该把它放在哪个班级吗?我不知道它是Native,SafeNative还是UnsafeNative.
我知道这可能是一个非常简单的问题,但我现在有一个脑屁.我正在尝试创建一个可以采用2种自定义类型之一的方法.基本上这个方法的主体对于两种类型都是相同的,因为它们都具有Name属性(我在Name属性上进行比较以用于排序).我该怎么做?
我的第一个想法是用两种类型作为参数重载方法:
int Compare(Type1 first, Type1 second)
int Compare (Type2 first, Type2 second)
Run Code Online (Sandbox Code Playgroud)
但这些方法的主体最终是相同的,因此它似乎是一种浪费.
我的下一个想法是使用泛型,但这似乎并不正确,因为我不是真正使它通用,因为它只能用于2种特定类型.
澄清:"自定义"类型实际上不是我的自定义类型.我的意思是他们不是内置类型.我无法控制这些类型或继承层次结构中的内容.他们碰巧都有Name属性.
我有一个后台工作者,在DoWork方法中我有以下内容:
var clipboardData = Application.Current.Dispatcher.Invoke(new Action(() => { Clipboard.GetData(DataFormats.Serializable); }));
Run Code Online (Sandbox Code Playgroud)
为什么即使我知道剪贴板上的数据格式正确,它总是返回null?
c# ×9
wpf ×5
datatrigger ×2
mstest ×2
clipboard ×1
combobox ×1
datagrid ×1
file-io ×1
fxcop ×1
generics ×1
isenabled ×1
itemscontrol ×1
mvvm ×1
overloading ×1
styles ×1
triggers ×1
unit-testing ×1
validation ×1