小编too*_*too的帖子

用于将Delphi TDataSet导出到本机XLS而不安装Excel的开源组件或单元

你是否知道存在一个免费的开源Delphi代码来将TDataSet导出为原生XLS格式?这个问题之前曾在这里提出,但我正在寻找一个免费的解决方案.

正如在TeeChart.TeeStore.TSeriesDataXLS.SaveToStream中观察到的那样,编写本机XLS导出函数并不是一门火箭科学,但我很难找到现有的解决方案,这肯定会节省一些时间和金钱(对于将用于的商业组件) 10-20%).

如果这样的导出代码允许颜色和文本样式更改将是有益的,但对我个人来说并不重要.

delphi excel components export export-to-excel

13
推荐指数
2
解决办法
9611
查看次数

如何在模糊匹配的字符串中查找子字符串的位置

我遇到了一个问题,即在OCR识别的文本中匹配字符串并找到它的位置,考虑到可以对错误,缺失或额外字符进行任意容忍.结果应该是最佳匹配位置,可能(不一定)匹配子串的长度.

例如:

String: 9912, 1.What is your name?
Substring: 1. What is your name?
Tolerance: 1
Result: match on character 7

String: Where is our caat if any?
Substring: your cat
Tolerance: 2
Result: match on character 10

String: Tolerance is t0o h1gh.
Substring: Tolerance is too high;
Tolerance: 1
Result: no match
Run Code Online (Sandbox Code Playgroud)

我试图改编Levenstein算法,但它不适用于子串并且不返回位置.

Delphi中的算法是首选,但任何实现或伪逻辑都可以.

delphi string fuzzy-search

8
推荐指数
1
解决办法
3164
查看次数

在WPF DataGrid中实现自定义复制和粘贴,当它没有行时可以工作

我需要为WPF应用程序中的网格之间的数据实现数据(非文本或CSV)的自定义复制+剪切+粘贴.使用标准ApplicationCommands并定义CommandBinding非常有效,但前提是DataGrid包含至少1行数据以及何时选择.当没有行或焦点不在任何行上时,所有命令都被禁用.

为了解决这个问题,我尝试在DataGrid上调用CommandManager.InvalidateRequerySuggested()并设置Focusable = true和/或FocusManager.IsFocusScope = true,但似乎内部DataGrid作为一个整体在处理复制/粘贴操作时"不感兴趣",只有它的行正在重新查询命令CanExecute状态并相应地调用Execute.它也忽略了KeyBindings.

如何让DataGrid处理重新查询ApplicationCommands?

请找到我在下面测试问题的示例:

<Window x:Class="WpfApplication1.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">
    <DataGrid x:Name="TheGrid">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Number" Binding="{Binding}"/>
        </DataGrid.Columns>
        <DataGrid.InputBindings>
            <KeyBinding Key="A" Command="{x:Static ApplicationCommands.New}"/>
        </DataGrid.InputBindings>
        <DataGrid.CommandBindings>
            <CommandBinding Command="{x:Static ApplicationCommands.Paste}" CanExecute="CanPaste" Executed="Paste"/>
            <CommandBinding Command="{x:Static ApplicationCommands.Copy}" CanExecute="CanCopy" Executed="Copy"/>
            <CommandBinding Command="{x:Static ApplicationCommands.New}" CanExecute="CanAddNew" Executed="AddNew"/>
        </DataGrid.CommandBindings>
        <DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Command="{x:Static ApplicationCommands.Copy}" Header="Copy"/>
                <MenuItem Command="{x:Static ApplicationCommands.Paste}" Header="Paste"/>
                <MenuItem Command="{x:Static ApplicationCommands.New}" Header="New row"/>
            </ContextMenu>
        </DataGrid.ContextMenu>
    </DataGrid>
</Window>
Run Code Online (Sandbox Code Playgroud)

而背后的代码:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class MainWindow …
Run Code Online (Sandbox Code Playgroud)

c# wpf routed-commands commandbinding wpfdatagrid

6
推荐指数
1
解决办法
1万
查看次数

当清单作为资源添加时,Delphi 5会导致EAccessViolation

这是我最近发现的最有趣的问题之一.我们有一个传统的Delphi 5程序(Rave Reports 4引用阻止升级到D2007).

使用模板生成的版本资源编译程序时,它可以正常工作.当模板生成的清单资源也添加到程序的dpr时,就会出现问题.

Manifest是一个"通用"ASCII文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">

  <assemblyIdentity
    name="Name"
    processorArchitecture="x86"
    version="2.0.0.0"
    type="win32"/>

  <description>Desc</description>

  <dependency>
      <dependentAssembly>
          <assemblyIdentity
              type="win32"
              name="Microsoft.Windows.Common-Controls"
              version="6.0.0.0"
              processorArchitecture="x86"
              publicKeyToken="6595b64144ccf1df"
              language="*"
          />
      </dependentAssembly>
  </dependency>

  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
    <application> 
      <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
    </application> 
  </compatibility>

</assembly>
Run Code Online (Sandbox Code Playgroud)

在App.dpr中有一个清单资源引用:

{$R 'manifest.res' 'manifest.rc'}
Run Code Online (Sandbox Code Playgroud)

通过调用编译清单:

C:\Program Files\Borland\Delphi5\Bin\brcc32.exe manifest.rc
Run Code Online (Sandbox Code Playgroud)

当程序启动时,会引发异常:

exception class   : EAccessViolation
exception message : Access violation at address 75A1A890 in module 'KERNELBASE.dll'. Read of address 00000001.
Run Code Online (Sandbox Code Playgroud)

主线程的调用堆栈: …

delphi compatibility exception comctl32 manifest

5
推荐指数
1
解决办法
2340
查看次数

Ribbon控件的WPF数据绑定

我有一个工作的动态菜单,它是绑定到由应用程序动态控制的分层项目集合的数据.以下参考是WPF声明:

        <Menu Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Actions}" Style="{StaticResource ResourceKey=dynamicMenu}">
       <Menu.Resources>
            <HierarchicalDataTemplate DataType="{x:Type wm:AppAction}" ItemsSource="{Binding Path=Items}">
                <HierarchicalDataTemplate.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="IsCheckable" Value="{Binding IsCheckable}" />
                        <Setter Property="IsChecked" Value="{Binding IsChecked}" />
                        <Setter Property="Visibility" Value="{Binding Path=IsVisible, Converter={wc:BoolToCollapsedConverter}}"/>
                        <Setter Property="Command" Value="{Binding Command}" />
                        <Setter Property="Icon" Value="{Binding Image}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Text}" Value="">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type MenuItem}">
                                            <Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </HierarchicalDataTemplate.ItemContainerStyle>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding ImageSource}" />
                    <TextBlock Text="{Binding Text}" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </Menu.Resources>
    </Menu>
Run Code Online (Sandbox Code Playgroud)

现在我想使用Microsoft Ribbon控件渲染这个底层菜单结构,最初使用RibbonTab和RibbonGroup为级别0和RibbonButton为级别1(每个选项卡上的单个组)构建它.

不幸的是由于某种原因它没有显示任何东西.这是我到目前为止所说的内容的宣言: …

data-binding wpf ribbon

5
推荐指数
1
解决办法
4337
查看次数

使用XamlReader和XamlWriter时,将一个FlowDocument的内容插入到另一个

我将FlowDocument与BlockUIContainer和InlineUIContainer元素一起使用,这些元素包含(或作为基类)一些自定义块-SVG,数学公式等。因此,使用Selection.Load(stream,DataFormats.XamlPackage)不能正常工作,因为序列化将删除*的内容UIContainers,除非Child属性是Microsoft参考源中提供的图像:

private static void WriteStartXamlElement(...)
{
    ...
    if ((inlineUIContainer == null || !(inlineUIContainer.Child is Image)) &&
                (blockUIContainer == null || !(blockUIContainer.Child is Image)))
    {
        ...
        elementTypeStandardized = TextSchema.GetStandardElementType(elementType, /*reduceElement:*/true);
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,唯一的选择是使用可以完美运行的XamlWriter.Save和XamlReader.Load,序列化和反序列化FlowDocument的所有必需属性和对象,但必须手动实现Copy + Paste作为Copy +的默认实现粘贴使用Selection.Load / Save。

复制/粘贴非常重要,因为它还用于处理RichTextBox控件中或控件之间的元素拖动-无需自定义拖动代码即可操作对象的唯一方法。

这就是为什么我要使用FlowDocument序列化实现复制/粘贴,但是不幸的是,它存在一些问题:

  1. 在当前解决方案中,需要对整个FlowDocument对象进行序列化/反序列化。从性能角度来看,这应该不是问题,但我需要存储信息,需要从中粘贴选择范围(CustomRichTextBoxTag类)。
  2. 显然,无法将对象从一个文档中删除并添加到另一个文档中(我最近发现了一个死胡同):'InlineCollection'元素无法插入树中,因为它已经是树的子级了。

    [TextElementCollection.cs]
    public void InsertAfter(TextElementType previousSibling, TextElementType newItem)
    {
        ...
        if (previousSibling.Parent != this.Parent)
            throw new InvalidOperationException(System.Windows.SR.Get("TextElementCollection_PreviousSiblingDoesNotBelongToThisCollection", new object[1]
            {
                (object) previousSibling.GetType().Name
            }));
        ...
    }
    
    Run Code Online (Sandbox Code Playgroud)

    我想考虑在所有需要移到另一个文档的元素中使用反射来设置FrameworkContentElement._parent,但这是不得已而又肮脏的解决方案:

  3. 从理论上讲,我只能复制所需的对象:(可选)在选择的开头部分运行带有文本的文本,在and之间的所有段落和内联以及(可能)在结尾部分运行的部分,将它们封装在自定义类中,并使用进行序列化/反序列化XamlReader / XamlWriter。

  4. 我没想到的另一个解决方案。

这是带有部分工作的自定义复制/粘贴代码的自定义RichTextBox控件实现:

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents; …
Run Code Online (Sandbox Code Playgroud)

c# wpf flowdocument xamlreader xamlwriter

5
推荐指数
1
解决办法
1736
查看次数

如何以编程方式在两个单独的窗口上添加DependencyProperty对象之间的WPF绑定

我遇到的问题可能很容易解决,但从初学者的角度来看似乎很棘手.

我创建了两个同时可见的窗口,一个带有ListBox,另一个带有TextBox.我想以编程方式将window1.ListBox.SelectedValue绑定到window2.TextBox.Text属性.我没有使用价值转换器.

代码很简单:

      var binding = new Binding("SelectedValue");
      binding.Source = window1.ListBox;
//      binding.Path = new PropertyPath(ListBox.SelectedValueProperty);
      var bound = window2.TextBox.SetBinding(TextBlock.TextProperty, binding);
Run Code Online (Sandbox Code Playgroud)

出于性能原因,我想使用DependencyProperty实例而不是属性名绑定,但我也尝试按名称绑定.绑定只是不起作用.

你可以在这里下载虚拟VS2010项目.

这导致了另一个问题 - 如何调试绑定,订阅哪个事件(如果有)以检测绑定更改?

c# wpf binding dependency-properties

3
推荐指数
1
解决办法
2万
查看次数

WPF将基于几何的图像绑定到MenuItem.Icon属性

我确信其他人找到了一种方法来做到这一点,我很接近但不完全.我使用StaticResourceConverter类成功地使用基于资源名称绑定到资源的Geometry的Path对象:

public class StaticResourceConverter: BaseConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(System.Convert.ToString(value).IsNullOrEmpty())
        {
            return null;
        }
        var resource = Application.Current.Resources[value];
        return resource;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}
Run Code Online (Sandbox Code Playgroud)

绑定到MenuItem中的Image对象需要使用相同的方法创建具有底层DrawingImage的Image实例:

<Style TargetType="{x:Type MenuItem}">
    <Setter Property="Icon">
        <Setter.Value>
            <Image Style="{StaticResource ResourceKey=SpMenuItemIcon}">
                <Image.Source>
                    <DrawingImage>
                        <DrawingImage.Drawing>
                            <GeometryDrawing
                                Brush="{StaticResource SpIconPath}"
                                Geometry="{Binding IconPath,
                                    Converter={converters:StaticResourceConverter}}"/>
                        </DrawingImage.Drawing>
                    </DrawingImage>
                </Image.Source>
            </Image>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

问题是我怀疑只创建了一个Image实例,或者只绑定了1个实例,因为只有菜单中的最后一个图标才能显示正确的几何路径.为什么WPF仅绑定最后一个MenuItem以及如何使其适用于所有菜单项?

更新: …

.net c# wpf binding menu

3
推荐指数
2
解决办法
3799
查看次数

如何在加载后使用POCO和DbContext在Entity Framework中初始化对象?

我正在使用EF5.我想初始化DbContext中包含的DbSet集合的每个对象,最好(但不一定)在对象字段(但不一定是与其他POCO的关系)之后初始化.这可能缩短为"施工后拦截"或"后加载拦截".

初始化需要使用与DbContext实例并行的对象实例(当前用户设置等)设置对象的某些属性.

所以基本上:

var settings = new Settings();
settings.PrintName = false;

var context = new MyDbContext();
// here initialize context so that every User object get initialized with settings
var john = context.Users.Where(x => x.Name == "John").FirstOrDefault();
Assert.AreEqual(john.Settings, settings);
Run Code Online (Sandbox Code Playgroud)

除了循环遍历集合中的所有对象并手动设置属性之外,实现此目的的可能方法是什么?

c# entity-framework poco dbcontext entity-framework-5

2
推荐指数
1
解决办法
3601
查看次数

使用消息在Windows中控制Wordpad

我试图找到一种可靠的方法来控制Windows Wordpad,我成功地将其作为子窗口嵌入到应用程序中.

我想在文档打开后执行的一个命令是"打印预览".PostMessage似乎是一个很好的工具,但我很难找到Wordpad接受的命令参考但没有成功,更不用说可以在WindowsXP机器以及Windows Vista,7和8上运行的命令.

我特别感兴趣的命令列表是:

  • 打印
  • 打印预览
  • 关闭而不保存(以比杀死进程更优雅的方式)

有人可以分享一些关于使用消息控制Wordpad的提示,还是可能以不同的方式?

windows delphi winapi messages wordpad

1
推荐指数
1
解决办法
626
查看次数