我有一个类库 A 用于我的解决方案中的其他项目,如 B 和 C。
类库 A 根据预处理器指令的存在而表现不同,例如:
#if some_directive
// some code
#else
// some other code
#end
Run Code Online (Sandbox Code Playgroud)
如何在项目 B 中使用类库 A 启用some_directive但在项目 C 中使用禁用some_directive?
如何使用带有NON-Empty构造函数的ViewModel对UserControl进行datatemplate?
public PersonViewModel(Person person)
{
_person= person;
// do some stuff
}
Run Code Online (Sandbox Code Playgroud)
由于Ctor不为空,因此在Xaml中绑定它将崩溃.但是当我使用ViewModel的父/子关系时,我必须将person对象传递给ViewModel的构造函数...
你如何应对这种情况?
我的 WPF 项目中有一些简单的层次结构。
我有基类,称为产品。然后我从这个基类继承了两个类。然后我从他们两个继承了更多的类。
所以我有 Product 类,然后是 Book 和 Disk 类,它们继承自 Product 类,以及 RecipeBook 类、ProgramingBook 类和 AdventureBook 类,它们继承自 Book 类。
然后我创建集合并在那里添加一些书,比如 collection.Add(new RecipeBook() {bla bla bla});
最后我收集了不同的类。
所以我需要的只是通过 WPF TreeView 显示所有这些东西。
它会是这样的:
Product
Book
RecipeBook
Book#1
Book#2
ProgramingBook
Book#1
....
Disk
Disk#1
Disk#2
.....
Run Code Online (Sandbox Code Playgroud)
那我该怎么做呢?
<TreeView x:Name="treeView" Height="224" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="337" >
<TreeViewItem Header="Products" >
<TreeViewItem Header="Books">
<TreeViewItem Header="Recipe"></TreeViewItem>
<TreeViewItem Header="Programing"></TreeViewItem>
<TreeViewItem Header="Adventure"></TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="CDs">
</TreeViewItem>
</TreeViewItem>
</TreeView>
Run Code Online (Sandbox Code Playgroud) 我试图知道是否有办法让DateTime对象在设置为GMT时间的操作系统上获得所有正确的属性.不只是改变时间.
现在我正在做的是:
DateTime f = new DateTime(2012, 8, 1); //A date on summer
DateTime f2 = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(f, TimeZoneInfo.Local.Id, "Central Europe Standard Time");
var isSummer = f2.IsDaylightSavingTime(); //It always returns false if the OS is on GMT
Run Code Online (Sandbox Code Playgroud)
它改变了时间和日期,使GMT成为中欧标准时间就好了,但IsDaylightSavingTime功能不会起作用.它总是返回false,因为我在系统上使用GMT,但我认为不应该因为我DateTime为中欧创建了一个.
有没有办法让DateTime对象f2真正本地化并让它理解如果它的八月并且是欧洲中心它应该返回true而不是false?
我知道如果我在机器上使用本地时间它可以正常工作,但我无法改变服务器上的GMT时间.我很乐意,但我不能.:-)
我在我的项目中使用了AvalonEdit控件.当我使用Ctrl + C或Ctrl + V等快捷键时,相关的复制/粘贴命令可以正常工作.我决定在上下文菜单中使用这些命令以获得更多可用性,因为有些用户习惯于右键单击而不是快捷方式.我使用以下XAML代码进行控制:
<avalonedit:TextEditor.ContextMenu>
<ContextMenu>
<MenuItem Command="Undo" />
<MenuItem Command="Redo" />
<Separator/>
<MenuItem Command="Cut" />
<MenuItem Command="Copy" />
<MenuItem Command="Paste" />
</ContextMenu>
</avalonedit:TextEditor.ContextMenu>
Run Code Online (Sandbox Code Playgroud)
但是当我运行程序时,这些命令总是在上下文菜单中显示为禁用,如下所示:

当我第一次遇到这个问题时,我发布了一个不同的问题但是在MD.Unicorn的帮助下(正如你在下面的评论中看到的)我意识到当你将AvalonEdit放在ListBox或ListView命令的ItemTemplate中时不起作用.
在MD.unicorn的帮助下,我创建了以下测试代码来重现结果:
ViewModel类和数据模板的简单类
public class MyViewModel : INotifyPropertyChanged
{
public MyViewModel()
{
collection = new ObservableCollection<myClass>();
mc = new myClass();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propName)
{
var h = PropertyChanged;
if (h != null)
h(this, new PropertyChangedEventArgs(propName));
}
public ObservableCollection<myClass> collection { get; set; }
public myClass …Run Code Online (Sandbox Code Playgroud) 我在Window的Resources部分中有一个DataTemplate,它创建一个带有ContextMenu的TextBlock.我希望能够设置ContextMenu中的MenuItem是否可以从我的Window视图模型中看到.我尝试通过设置访问Window的DataContext ElementName,并尝试设置RelativeSource,但这两种方法都导致了绑定错误.我不确定我还能尝试什么.
我创建了一个小例子来展示我正在尝试做的事情:
XAML:
<Window x:Class="DataTemplateTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="TestDataTemplate">
<TextBlock Text="{Binding}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Test" Visibility="{Binding Path=DataContext.MenuItemVisible, ElementName=Root}"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<ScrollViewer x:Name="Root">
<ItemsControl ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource TestDataTemplate}" />
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
namespace DataTemplateTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
protected readonly MainWindowViewModel vm;
public MainWindow()
{
InitializeComponent();
vm = new MainWindowViewModel(); …Run Code Online (Sandbox Code Playgroud) 我有 3 个这样的字符串数组:
public string[] RowHeaders
{
get { return new[] {"RowHeader1", "RowHeader2", "RowHeader3", "RowHeader4"};
}
public string[] ColumnHeaders
{
get { return new[] {"ColumnHeader1", "ColumnHeader2", "ColumnHeader3"}; }
}
public string[][] Values
{
get { return new[]
{
new []{"Value11", "Value12", "Value13"},
new []{"Value21", "Value22", "Value23"},
new []{"Value31", "Value32", "Value33"},
new []{"Value41", "Value42", "Value43"},
}; }
}
Run Code Online (Sandbox Code Playgroud)
数组大小在运行时之前是未知的(代码片段中的数组值用于展示概念)。我想从它们创建一个 WPF 网格,例如
它绑定到这 3 个数组并完全在 XAML 中设计(如果可能)。如何?
我创建了一个附加属性来扩展具有附加状态的Button类:
<Button v:ExtensionHelper.OperationMode="{Binding MyObject.OperationMode}"
Command="{Binding MyObject.Select}"
Style="{StaticResource operationModeControlTemplateStyle}" />
Run Code Online (Sandbox Code Playgroud)
然后,我想在访问此值,ControlTemplate使用DataTrigger这样的:
<Style x:Key="operationModeControlTemplateStyle" TargetType="Button">
<Setter Property="IsHitTestVisible" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Image x:Name="hand" Source="hand.png" />
<Image x:Name="cross" Source="cross.png" />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=(v:ExtensionHelper.OperationMode), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Value="Manual">
<Setter TargetName="cross" Property="Visibility" Value="Collapsed" />
<Setter TargetName="hand" Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=(v:ExtensionHelper.OperationMode), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Value="Disabled">
<Setter TargetName="cross" Property="Visibility" Value="Visible" />
<Setter TargetName="hand" Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=(v:ExtensionHelper.OperationMode), RelativeSource={RelativeSource FindAncestor, …Run Code Online (Sandbox Code Playgroud) data-binding wpf datatrigger controltemplate attached-properties
我想在我的 WPF 应用程序中创建一个通用数据类型转换器,比如这个类模板:
[ValueConversion(typeof(T), typeof(string))]
class DataTypeConverter<T> : IValueConverter
Run Code Online (Sandbox Code Playgroud)
将 int、double、byte、ushort 等数据类型转换为字符串,反之亦然,并在我的类的不同属性类型的两种绑定中使用它,然后通过这行代码简单地创建任何类型:
class ushortTypeConverter : DataTypeConverter<ushort>{}
Run Code Online (Sandbox Code Playgroud)
如果可能,在错误输入的情况下绑定的文本框中显示验证消息。
是否可以编写这样的类模板?
我刚开始学习c#而且我遇到了令人困惑的事情,我希望有人可以向我解释:)
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int Md = 0;
private void cmdAnzeigen1_Click(object sender, EventArgs e)
{
int x = 0;
Md = Md + 1;
x = x + 1;
lblErgebnis.Text = "x: " + x + "\n" + "Gx:" + Md;
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么Md总是增加1,但x不增加(保持在1)?我不明白,它们都是相同的变量.
c# ×7
wpf ×7
.net ×2
binding ×2
xaml ×2
arrays ×1
avalonedit ×1
build ×1
constructor ×1
data-binding ×1
datagrid ×1
datatemplate ×1
datatrigger ×1
datetime ×1
generics ×1
hierarchy ×1
treeview ×1
viewmodel ×1