小编Cod*_*ked的帖子

C#:无法撤消插入的文本

我使用KeyPress事件以编程方式在自定义RichTextBox中添加文本:

SelectedText = e.KeyChar.ToString(); 
Run Code Online (Sandbox Code Playgroud)

问题是以这种方式插入文本不会触发CanUndo标志.

因此,当我尝试撤消/重做文本时(通过调用文本框的Undo()和Redo()方法),没有任何反应.

我尝试以编程方式从TextChanged()事件中唤起KeyUp()事件,但仍未将CanUndo标记为true.

如何撤消插入的文本而不必为撤消和重做操作创建列表?

谢谢

c# undo redo winforms

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

无法在PowerShell中加载XAML

我有一个非常基本的PowerShell脚本,可以加载xaml布局.它抛出了这个错误:

用"1"参数调用"Load"的异常:
"无法创建
程序集'PresentationFramework,Version = 3.0.0.0,
Culture = neutral,PublicKeyToken = 31bf3856ad364e35'中定义的'Window'实例.
调用线程必须是STA,因为很多UI组件都需要这个."

我已经多次查看了脚本和xaml文件,但是我没有看到问题出在哪里,并且我无法完全理解我所获得的错误也无济于事.任何帮助将非常感激.谢谢.

Powershell脚本:

Add-Type -AssemblyName presentationframework
$xaml = [xml](Get-Content MainWindow.xaml)
$reader = New-Object System.Xml.XmlNodeReader $xaml
$form = [Windows.Markup.XamlReader]::Load($reader)
$button = $form.FindName('testButton')
$button.Add_Click({
    $s = $form.FindName('testBox').Text;
    $form.FindName('testLabel').Content = $s;
})
$form.ShowDialog()
Run Code Online (Sandbox Code Playgroud)

Xaml文件:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot">
        <TextBox x:Name="testBox" HorizontalAlignment="Left"
                 Margin="26.782,48.626,0,0" TextWrapping="Wrap"
                 Text="New label text" VerticalAlignment="Top" Width="130.22"/>
        <Label x:Name="testLabel" Content="User Search" HorizontalAlignment="Left"
                 Margin="20.666,22.666,0,0" VerticalAlignment="Top"/>
        <Button x:Name="testButton" Content="Change It!" HorizontalAlignment="Left"
                 Margin="26.782,85,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

wpf powershell xaml

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

为什么Visual Studio在异常期间引用了错误的行?

下面是一些简单的代码行:

string[] countries = new string[1];
string[] cities = new string[1];

countries[0] = "USA";
countries[1] = "England";
cities[0] = "Chicago";  
Run Code Online (Sandbox Code Playgroud)

当Visual Studio抛出null异常时,它引用以下行而不是发生异常的行:
在此输入图像描述

当您查看问题的详细信息时,我知道行号是正确的.但是,我经常花费更多的时间来搜索错误的变量,因为异常的可视化表示是错误的.

是否有可能调整VS或做其他事情来解决这个问题?这是一个已知问题还是仅仅是我?

c# exception visual-studio-2010

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

绑定到列表框时出现XamlParseException

在我的应用程序中,我允许用户将相机和照片库中的照片保存到隔离存储中.然后我获取每个文件的名称并阅读照片并添加到我的列表中.构建列表后,我将其绑定到列表框.

我可以大约5显示没有问题.滚动后我得到异常:

System.Windows.Markup.XamlParseException occurred
  Message= [Line: 0 Position: 0]
   --- Inner Exception ---
KeyNotFoundException
Run Code Online (Sandbox Code Playgroud)

这是我的XAML:

<ListBox x:Name="userPhotosListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
                <ContentControl Content="{Binding Image}" Width="400" />
                <Image Name="{Binding FileName}" Source="/Images/appbar.delete.rest.png" Width="48" Height="48"
                    MouseLeftButtonUp="Image_MouseLeftButtonUp" VerticalAlignment="Center" HorizontalAlignment="Center" MaxWidth="48" MaxHeight="48" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

这是代码:

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    var userFiles = store.GetFileNames();
    foreach (var userFile in userFiles)
    {
        if (userFile.Contains(PhotoInIsolatedStoragePrefix))
        {
            var currentBitmap = ReadBitmapImageFromIso(userFile);
            var userPhotoImage = new Image { Source = currentBitmap };
            var userImg = …
Run Code Online (Sandbox Code Playgroud)

c# xaml xamlparseexception windows-phone-7

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

如何在运行时调整wpf中的用户控件

我是wpf开发的新手.在我的wpf应用程序中,我使用了用户控件.我需要的是在运行时如何调整用户控件的大小,

请帮我找到解决方案,如果我的方法有误,请指导我解决,

我的XAML代码是

<UserControl x:Class="test.pad"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Focusable="True" OverridesDefaultStyle="False">

    <Grid Width="102" Height="123" Name="Grid1" MouseMove="Grid1_MouseMove" MouseUp="Grid1_MouseUp" MouseDown="Grid1_MouseDown">
        <Grid.RenderTransform>
            <TranslateTransform x:Name="tt"/>
        </Grid.RenderTransform>

        <Border BorderBrush="Silver" BorderThickness="2">

            <Canvas Height="120" HorizontalAlignment="Left" Margin="0,1,0,0" Name="canvas1" VerticalAlignment="Top" Width="100" Grid.ColumnSpan="2">
                <RichTextBox Height="98" HorizontalAlignment="Left" Margin="0,24,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="100" BorderThickness="2" Grid.ColumnSpan="2" Background="#FFFF96D8" Canvas.Top="-3" Canvas.Left="-1" />
                <Button BorderBrush="#FFFF96D8" Canvas.Left="46" Canvas.Top="-2" Height="23" Name="close" Width="27">

                </Button>
                <Button Height="23" Name="minimise" Width="27" BorderBrush="#FFFF96D8" Click="button1_Click" Canvas.Left="72" Canvas.Top="-2" ClipToBounds="False" IsEnabled="True" IsHitTestVisible="True" OverridesDefaultStyle="False" ForceCursor="False" Foreground="Black" MouseMove="button1_MouseEnter" MouseLeave="button1_MouseLeave">

            </Canvas>

        </Border>

    </Grid> …
Run Code Online (Sandbox Code Playgroud)

c# user-controls wpf-controls visual-studio-2008

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

加载FlowDocument.xaml,它是我的解决方案的一部分

我在当前的WPF项目中创建了一个FlowDocument.xaml.我想要做的是当用户单击一个按钮时,XAML文档将被加载到后面的代码中,修改文档上的一些数据,然后将其打印出来.关键点是我不知道如何加载流文档,以便我可以修改它.

当我做:

FileStream fs = File.Open("FlowDocument.xaml", FileMode.Open)
Run Code Online (Sandbox Code Playgroud)

它说它找不到文件.该文件是项目的一部分,我猜它在编译时会与项目的其余部分打包在一起.

任何帮助表示赞赏

c# wpf xaml flowdocument

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

WPF:是否有一种 XAML 标记的行为类似于 HTML span 元素?

来自 ASP.NET,这个 WPF 的东西只是令人困惑。我想做的就是在标签旁边放置一个红色星号来指示必填字段。尝试了一下,我发现这实际上可以解决问题:

<TextBlock Grid.Row="6" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Label Foreground="Red" Content="*" /><Label Content="Heavy Weight" />
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

由于我刚刚想到这个,我不相信这是经验丰富的 WPF 开发人员会采取的学术路线。此外,此标记在星号和标签之间放置了大量空白。在 HTML 中,span 元素只会在其下一个同级元素旁边呈现。仅供参考,我尝试将标签放入标签中,但 VS2010 一直抱怨“属性‘内容’被设置多次”。

有任何想法吗?

wpf field required

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

转换DateTime会导致IndexOutOfRangeException

Time =(DateTime) AllQuestionsPresented.TryParse(dr["Time"].ToString());
Run Code Online (Sandbox Code Playgroud)

哪里dr是SqlReader.

我得到了一个IndexOutOfRangeException,我不知道为什么.这是TryParse函数:

public static DateTime? TryParse(string text)
{
    DateTime date;
    if (DateTime.TryParse(text, out date))
    {
        return date;
    }
    else
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我得到那个例外,我怎么能摆脱它?!

c# sql indexoutofboundsexception

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

物业访问者

两者之间有什么根本区别

public string Name  
{  
  get  
  {  
    return _name;  
  }  
  set  
  {  
    _name = value;  
  }  
} 
Run Code Online (Sandbox Code Playgroud)

public string Name {get; set;}
Run Code Online (Sandbox Code Playgroud)

c# oop properties object

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

WPF MVVM事件订阅

为简单起见,我们有一个modelview(MyModelView),它包含一个Scheduler属性,里面有一个计时器(Scheduler).计时器用于检查当前时间,如果它们匹配,则发出"事件匹配"信号,表示模型视图应以某种方式通知.现在MyModelView知道调度程序,但不是相反.

public Scheduler()
    {
        ScheduleCollection = new ObservableCollection<Schedule>();

        TimeSpan ts = new TimeSpan(30000);

        _timer = new DispatcherTimer();
        _timer.Interval = ts;
        _timer.Tick += new EventHandler(EventTimerCheck_Tick);
        _timer.Start();

    }
private void EventTimerCheck_Tick(object sender, EventArgs e) 
    {
        eventsToLaunch = LocateCurrentEvents();

        if (eventsToLaunch.Count > 0) { RaiseHasEvents(); }
    }

public void RaiseHasEvents()
    {
       EventHandler handler = this.HasEvents;
        if (handler != null)
        {
            var e = new EventArgs();
            handler(this, e);
        }
    }

public event EventHandler HasEvents;
Run Code Online (Sandbox Code Playgroud)
public MyModelView()
   {

       Scheduler scheduler = new Scheduler();

       HaveEvents += …
Run Code Online (Sandbox Code Playgroud)

c# wpf events mvvm

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