在我看来,这似乎是一个有趣的问题 -
为什么IsCheckedWPF类型bool?(或Nullable<bool>)中的复选框控件的属性,我的意思是复选框控件如何具有'null'的值?
我使用 MVVM 编写了一个大型 WPF 应用程序,但它遇到了一些间歇性问题。我最初询问“已添加具有相同键的项目”从代码问题中选择 ListBoxItem 的异常,这解释了问题,但没有得到答案。
一段时间后,我设法找出了Exception我得到的原因,并将其记录在没有不可变字段的类中重写 Object.GetHashCode() 时返回什么?问题。基本上,这是因为我在公式中使用了可变字段来返回 的值GetHashCode。
从我收到的针对该问题的非常有用的答案中,我设法加深了对该领域的理解。以下是三个相关规则:
这些规则影响了我对不知道从GetHashCode方法返回什么的问题的可能解决方案:
readonly为每个类创建一个附加字段,而只能在该方法中使用。GetHashCode我最终采用的解决方案是在ObservableCollection编辑方法中使用的任何属性之前从其中删除每个项目GetHashCode,然后再次重新添加它。虽然到目前为止,这在许多视图中都运行良好,但我遇到了进一步的问题,因为我的 UI 项目是使用自定义动画的Panel。当我重新添加一个项目时(即使将其插入到集合中的原始索引中),它会再次启动条目动画。
我已经添加了一些基类方法,例如AddWithoutAnimation, RemoveWithoutAnimation,这有助于解决其中一些问题,但它不会影响任何Storyboard动画,重新添加后仍然会触发动画。最后,我们来回答这个问题:
首先,我想明确声明,我没有Dictionary在代码中使用任何对象...Dictionary抛出 的对象必须Exception是ObservableCollection<T>. 我上一个问题中似乎大多数人都忽略了这一点。因此,我不能选择干脆不使用Dictionary……如果可以的话。 …
我想知道如何读取位于服务器上的多个(大约500-1000)个文本文件.到目前为止,我已经为只读取单个文本文件的程序编写了代码.
这是我目前正在阅读单个文件的方式.
public void button1_Click(object sender, EventArgs e)
{
// Reading/Inputing column values
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] fileLines = File.ReadAllLines(ofd.FileName);
Run Code Online (Sandbox Code Playgroud)
我想摆脱打开文件对话框,让程序自动读取位于服务器中的500-1000文本文件.
我正在思考一些事情
for (int i =0; i<numFiles; i++)
{
//just use string[] fileLines =File.ReadAllLines()
//how would i specify the path for multiple files?
}
Run Code Online (Sandbox Code Playgroud)
那么问题是:
可以在WPF中定义快捷方式
<KeyBinding Key="N" Modifiers="Control" Command="local:CustomCommands.MyCommand"/>
Run Code Online (Sandbox Code Playgroud)
CTRL + N现在定义了快捷方式.
问题:是否也可以定义一个双键快捷方式,如Visual Studio 2012使用它?
示例: CTRL + R, A用于执行所有单元测试.
我有一个绑定到项目列表的 ItemsControl。这些项目具有名称和值属性。value 属性属于 Object 类型,以允许使用不同的数据类型。为了正确显示 value 属性,我将 ContentPresenter 与我可能使用的每种数据类型的数据模板一起使用。
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=Name}"/>
<GridSplitter Width="1"
Grid.RowSpan="4" Grid.Column="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<ContentPresenter Grid.Column="2" Content="{Binding Value}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type System:String}">
<TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
BorderThickness="0"/>
</DataTemplate>
<DataTemplate DataType="{x:Type System:Int32}">
<TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
TextAlignment="Right"
BorderThickness="0"/>
</DataTemplate>
<DataTemplate DataType="{x:Type System:Double}">
<TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
TextAlignment="Right"
BorderThickness="0"/>
</DataTemplate>
<DataTemplate DataType="{x:Type System:Boolean}">
<CheckBox IsChecked="{Binding Path=Content, RelativeSource={RelativeSource …Run Code Online (Sandbox Code Playgroud) 直到最近,我还使用了IDataErrorInfo接口的自定义扩展版本。我的扩展程序使我可以同时处理多个错误,到目前为止,它为我提供了很好的服务。但是,随着INotifyDataErrorInfo界面的引入,我想我将对其进行试验以查看是否有任何改进。
在阅读了一些在线教程之后,我从中获得了它与各种ValidationAttributes的配合使用System.ComponentModel.DataAnnotations namespace。使用这些,Attribute您可以提供如下基本验证规则:
[MinLength(3, ErrorMessage = "Name must be longer than 3 characters.")]
public string Name
{
get { return name; }
set { name = value; NotifyPropertyChanged("Name"); Validate("Name", name); }
}
Run Code Online (Sandbox Code Playgroud)
最初,它看起来还不错,因为错误消息直接插入Valaidation.Errors了Applied ErrorTemplates中可用的集合中。但是,大多数内置的验证规则实际上都是最基本的,我已经习惯于实现涉及其他属性值的复杂验证规则。
因此,我着手寻找一种创建包含多个属性的简单验证规则的方法:必须设置两个或多个字段之一的规则。因此,我声明了一个扩展的类,ValidationAttribute并在在线搜索后找到了一种访问其他属性值的方法。
我敲了一个基本UI,并ErrorTemplate在每个UI上应用了一个自定义TextBox,它显示Validation.Errors了数据绑定属性的集合:
<ControlTemplate x:Key="ErrorTemplate">
<StackPanel Orientation="Horizontal">
<Border BorderBrush="#4FFF0000" BorderThickness="1" Margin="0,10">
<AdornedElementPlaceholder />
</Border>
<Image Name="WarningImage" Source="pack://application:,,,/WpfApplication1;component/Images/Warning_16.png" Margin="5,0,0,0" Tag="{Binding}" />
<Popup PlacementTarget="{Binding ElementName=WarningImage}" Placement="Right" Margin="5,0,0,0" AllowsTransparency="True" …Run Code Online (Sandbox Code Playgroud) 我有一个自定义ClockFace UserControl,它具有允许更改颜色,字体和手(作为Path对象)的属性.这用于自定义TimePicker和Clock UserControls.在这些父控件中,可以在xaml中的ClockFace对象上设置ClockFace属性.我正在尝试做的是暴露这些ClockFace属性,以便可以在这两个父控件(例如,Clock和TimePicker对象)上设置它们.我认为将它们作为附加属性可以解决问题,所以我尝试使用其中一种颜色属性.
public static readonly DependencyProperty HourTicksBrushProperty = DependencyProperty.RegisterAttached("HourTicksBrush", typeof(Brush), typeof(ClockFace), new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender));
public static void SetHourTicksBrush(DependencyObject element, Brush value)
{
element.SetValue(HourTicksBrushProperty, value);
}
public static Brush GetHourTicksBrush(DependencyObject element)
{
return (Brush)element.GetValue(HourTicksBrushProperty);
}
Run Code Online (Sandbox Code Playgroud)
我可以在时钟所在的xaml中使用这个附加属性:(控件是xml命名空间)
<Controls:Clock Controls:ClockFace.HourTicksBrush="Aqua" />
Run Code Online (Sandbox Code Playgroud)
它编译得很好,但是虽然来自附加的HourTicksBrushProperty的默认值(Brushes.Black)显示,但在父时钟控件(Aqua)上设置的值永远不会触发上述方法或更改颜色.我错过了什么吗?
为了清楚起见,我希望能够在父控件上使用上面的xaml来设置子ClockFace控件的HourTicksBrush属性.
任何帮助将非常感激.
我的最终目标是在两个UserControl之间设置大小变化的动画.我试图实现这一目标的方式产生了一个主要问题.
我从一个包含一些基本文本和图标显示的DataTemplate开始,一个高度设置为0的'edit'UserControl和一个编辑按钮.编辑UserControl在GridRow中,Height ="Auto",因此它也以高度0开始.按钮具有由按钮单击触发的DoubleAnimation,用于将UserControl的高度设置为0到300.这一切都有效正好.这是一个简化的代码示例.
<DataTemplate x:Key="UserTemplate" DataType="{x:Type dataTypes:User}">
...
<controls:UserView Grid.Row="1" Grid.ColumnSpan="5" x:Name="EditRow"
DataContext="{Binding}" Height="0" />
<controls:UserEditor Grid.Row="2" Grid.ColumnSpan="5" x:Name="EditRow"
DataContext="{Binding}" Height="0" />
<Button Grid.Row="0" Grid.Column="4" Name="Edit" Style="{StaticResource ButtonStyle}"
ToolTip="Edit user" Click="Button_Click">
<Image Source="/SolutionName;component/Images/Edit.png" Stretch="None" />
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Name="EditRowHeightAnimation"
Storyboard.TargetName="EditRow" Storyboard.TargetProperty="Height" From="0"
To="300" Duration="00:00:0.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
...
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
问题是编辑UserControl不是300像素高,我不知道它在设计时的高度.我尝试了以下,但它不起作用.
<DoubleAnimation Name="EditRowHeightAnimation" Storyboard.TargetName="EditRow"
Storyboard.TargetProperty="Height" From="0" To="{Binding DesiredSize.Height,
ElementName=EditRow}" Duration="00:00:0.5" />
Run Code Online (Sandbox Code Playgroud)
我也尝试从后面的代码编辑UserControl上调用Measure()和UpdateLayout().我注释掉了按钮单击触发器和xaml动画,并在代码后面添加了一个......现在这种方式有效,但我总是得到相同(错误的)DesiredSize.也就是说,UserControl高度会变得动画,但只是到了错误的高度.这是按钮单击处理程序代码.
private void Button_Click(object sender, RoutedEventArgs e)
{
User currentUser = (User)CollectionViewSource.GetDefaultView(Users).CurrentItem;
ListBoxItem listBoxItem …Run Code Online (Sandbox Code Playgroud) 我有一个包含许多项目的大型解决方案,其中一个是安装项目.还有许多当前版本存储在单独的分支中.我有一个曾经在.NET 2中工作的构建工具,但自从我们升级到.NET 4以来一直没有用.
在内部,构建工具的新.NET 4版本使用Microsoft.TeamFoundation.Client.RegisteredTfsConnections.GetProjectCollections()并从我的TFS源控制服务器versionControlServer.GetAllTeamProjects(false)获取TeamProjects 的集合.
然后,我在UI中以可视方式显示它们,当用户单击特定解决方案版本时,应用程序调用以下内容以获取该解决方案版本的最新信息:
workspace.Get(new string[] { serverPath }, VersionSpec.Latest, RecursionType.Full,
GetOptions.GetAll);
Run Code Online (Sandbox Code Playgroud)
用于构建解决方案文件的应用程序,包括安装项目.在此阶段,安装项目将创建可以安装应用程序的MSI.这是我遇到问题的最后一步.
我需要能够以编程方式构建用户使用C#代码选择的解决方案.工作的.NET 2代码如下:
Process process = new Process();
ProcessStartInfo processStartInfo = process.StartInfo;
processStartInfo.FileName = processName;
processStartInfo.Arguments = string.Format(" \"{0}\" /BUILD \"Release|Any CPU\"",
solutionPath);
processStartInfo.WorkingDirectory = processDirectory;
process.Start();
Run Code Online (Sandbox Code Playgroud)
运行此命令时没有错误,但它不再启动Visual Studio并构建代码.很明显,这是一个很难做到最初的方法,但我找不到使用TFS类的"正确"方法.
我也尝试直接运行MSBuild.exe(类似于上面的例子),这确实构建了解决方案,但由于某种原因没有构建产生MSI的安装项目.请注意,我不使用任何手动创建的构建文件.
不幸的是,很难找到Microsoft.TeamFoundation命名空间的有用文档!我希望这里有人利用这些课程,并指导我解决这个问题.
如果可能的话,我需要使用.NET类(例如,不是Process.Start),因为我真的需要知道构建何时完成.但是FileSystemWatcher,如果这个问题太多,我可以为此设置一个对象.
如何在运行时更改另一个资源字典中使用的资源字典中的颜色?
这是我的设置:
Colours.xaml:
<SolidColorBrush x:Key="themeColour" Color="#16A8EC"/>
Run Code Online (Sandbox Code Playgroud)
Styles.xaml:
<Style x:Key="titleBar" TargetType="Grid">
<Setter Property="Background" Value="{DynamicResource themeColour}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
Window.xaml
.....
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="res/Styles.xaml"/>
<ResourceDictionary Source="res/Colours.xaml"/>
</ResourceDictionary.MergedDictionaries>
.....
<Grid Style="{DynamicResource titleBar}"></Grid>
Run Code Online (Sandbox Code Playgroud)
代码背后:
Application.Current.Resources["themeColour"] = new SolidColorBrush(newColour);
Run Code Online (Sandbox Code Playgroud)
当代码运行时,网格的颜色不会改变.我不认为Application.Current.Resources ["themeColour"]是指我的solidcolorbrush资源,因为如果我在分配新颜色之前尝试访问它,我会得到一个空对象引用异常.
那么,我该如何访问资源"themeColour"?
c# ×8
wpf ×8
xaml ×2
animation ×1
binding ×1
build ×1
checkbox ×1
datatemplate ×1
dependencies ×1
file ×1
gethashcode ×1
overriding ×1
shortcut ×1
solution ×1
tfs2010 ×1
validation ×1