小编Cha*_*u A的帖子

for和foreach有什么区别?

forforeach循环之间的主要区别是什么?

在哪些场景中我们可以使用for而不是foreach,反之亦然.

是否可以用简单的程序来展示?

两者对我来说都是一样的.我无法区分它们.

c# foreach for-loop

18
推荐指数
4
解决办法
11万
查看次数

如何使用LINQ to XML获取属性值?

<Employees>
  <Employee>
    <EmpId>1</EmpId>
    <Name>Sam</Name>
    <Sex>Male</Sex>
    <Phone Type="Home">423-555-0124</Phone>
    <Phone Type="Work">424-555-0545</Phone>
  </Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    emplyeeDetails = XDocument.Load(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\LinqToXml\\Xmls\\" + "Employees.xml");
    var emplyees = from emp in emplyeeDetails.Descendants("Employee").Take(10)
                   orderby emp.Element("EmpId").Value ascending
                   select new
                   {
                       Id = emp.Element("EmpId").Value,
                       Name = emp.Element("Name").Value,
                       Sex = emp.Element("Sex").Value,
                       WorkPhone=emp.Element("Phone").Attribute("Type").Value,
                       HomePhone = emp.Element("Phone").Attribute("Type").Value,                               
                   };
    DgrdEmployeeDetails.ItemsSource = emplyees.ToList();
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我可以得到以下结果: 在此输入图像描述

但我需要列WorkPhone的值424-555-0545而不是HomeHomePhone的值423-555-0124而不是列的值Home.

我该怎么办?

c# xml linq linq-to-xml

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

如何从资源字典中获取画笔并将其应用于动态元素在wpf中?

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<LinearGradientBrush x:Key="ButtonNormalBackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#C10099FF" Offset="0"/>
    <GradientStop Color="#C16699CC" Offset="1"/>
    <GradientStop Color="#C1006699" Offset="0.49"/>
</LinearGradientBrush>
<ResourceDictionary/>
Run Code Online (Sandbox Code Playgroud)

现在我想从ResourceDictonary获取LinearGradientBrush并将其动态应用于按钮作为wpf中的背景颜色.

 BtnGetBrushes.Background = Brushes.Green;
Run Code Online (Sandbox Code Playgroud)

我想应用上面的颜色而不是这个(Brushes.Green).我该怎么办?

c# wpf resourcedictionary

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

Textbox和RichTextbox wpf之间的区别?

Textbox和RichTextbox看起来都一样.但是不知道区别.请告诉我任何人,当我必须在wpf中使用TextBox和RichTextbox时.

wpf textbox richtextbox

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

如何在wpf中获取当前的应用程序路径

public XML()
{
    this.InitializeComponent();

    XmlDocument document_name = new XmlDocument();       
    XmlElement student = document_name.CreateElement("Student");
    XmlElement name = document_name.CreateElement("Chandru");
    student.AppendChild(name);
    document_name.AppendChild(student);
    XmlAttribute id = document_name.CreateAttribute("ID");
    name.SetAttributeNode(id);
    id.Value = "sst5038";
    XmlElement fname = document_name.CreateElement("FName");
    fname.InnerText = "Anjappn";
    name.AppendChild(fname);
    XmlElement mname = document_name.CreateElement("MName");
    mname.InnerText = "Thaiyamuthu";
    name.AppendChild(mname);
    document_name.AppendChild(student);
    document_name.Save(@"D:\student.xml");
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码.我在wpf中创建一个xml文件作为代码,我将此文件保存在我的本地磁盘D:\ student.xml中

 document_name.Save(@"D:\student.xml");
Run Code Online (Sandbox Code Playgroud)

但是我想将这个xml文件(student.xml)保存在我正在使用的项目文件中.

我该怎么做呢

请帮我...

c# wpf

7
推荐指数
2
解决办法
4万
查看次数

如何在xaml中连接按钮内容?

我试图Button在XAML 中将两个字符串连接为内容.但它不起作用.我尝试过如下:

<Button Content=""String1"+{DynamicResource String2}"/>
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml concatenation button

7
推荐指数
2
解决办法
4809
查看次数

如何通过wpf中的行和列获取网格子节点?

<Grid x:Name="LayoutRoot" HorizontalAlignment="Center" VerticalAlignment="Center" >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Button Width="150" Height="50" x:Name="Btn1" Content="Button1" Grid.Row="0" Grid.Column="0"/>
    <Button Width="150" Height="50" x:Name="Btn2" Content="Button2" Grid.Row="0" Grid.Column="1"/>
    <Button Width="150" Height="50" x:Name="Btn3" Content="Button3" Grid.Row="2" Grid.Column="0"/>
    <Button Width="150" Height="50" x:Name="Btn4" Content="Button4" Grid.Row="2" Grid.Column="1"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

wpf中的C#代码

Visual childVisual = (Visual)VisualTreeHelper.GetChild(LayoutRoot,0);
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我可以获得网格的第一个子节点(LayoutRoot).但是我想通过它的行或列获取网格子节点.我该怎么办呢.

提前致谢.

c# wpf grid xaml

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

单击wpf中的按钮时如何避免TextBox丢失焦点?

在此输入图像描述

现在光标聚焦在TextBox中.如果我单击按钮(RemoveLostFocus),TextBox的Lost焦点事件将被触发.但我需要的是,TextBox的Lost Focus事件不应该触发.有没有办法这样做?

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        txtUserName.Focus();
    }

private void UserName_LostFocus(object sender, RoutedEventArgs e)
    {
        if (txtUserName.Text.Length < 1)
        {
            MessageBox.Show("UserName should not be empty");
        }
    }

    private void btnCancel_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
                    anotherWindow.Show();
    }
Run Code Online (Sandbox Code Playgroud)

wpf textbox

4
推荐指数
2
解决办法
4333
查看次数

如何在wpf中动态更改资源字典源名称?

<Application x:Class="CustControls.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ControlLibrary;component/Styles/ControlResource.xaml"/>
            <ResourceDictionary Source="StringLocalization/Dictionary_fr-FR.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

现在我想将 ResourceDictionary 的源名称Source="StringLocalization/Dictionary_fr-FR.xaml"更改为 Source="StringLocalization/Dictionary_en-US.xaml"

我该怎么做。

wpf resourcedictionary

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

如何通过XAML将WPF中的小写转换为大写?

我尝试在WPF中通过XAML将大写转换为小写,如下所示:

<TextBox Height="86" CharacterCasing="Upper"/>
Run Code Online (Sandbox Code Playgroud)

我想实现用同样的场景TextBlock,LabelButton.

我该怎么做?

c# wpf xaml textbox lowercase

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

如何在代码隐藏中创建ResourceDictionary?

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <sys:String x:Key="one">ONE</sys:String>
    <sys:String x:Key="two">TWO</sys:String>
    <sys:String x:Key="three">THREE</sys:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

现在想要使用 C# 在 WPF 中通过代码隐藏动态创建与上面相同的资源 ResourceDictionary 。可以这样创建吗?

c# wpf xaml .net-4.0 resourcedictionary

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

在WPF中使用Regex的问题?

private void TxtName_TextChanged(object sender, TextChangedEventArgs e)
    {
            string getString = Regex.Replace(TxtName.Text, @"[a-z, A-z, 0-9]", string.Empty);

    } 
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我可以替换与模式@"[az,Az,0-9]" 匹配的字符串.但我需要的是,我想替换与模式@"[az,Az,0-9]" 不匹配的字符串.

c# regex wpf

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