for和foreach循环之间的主要区别是什么?
在哪些场景中我们可以使用for而不是foreach,反之亦然.
是否可以用简单的程序来展示?
两者对我来说都是一样的.我无法区分它们.
<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而不是Home列HomePhone的值423-555-0124而不是列的值Home.
我该怎么办?
<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).我该怎么办?
Textbox和RichTextbox看起来都一样.但是不知道区别.请告诉我任何人,当我必须在wpf中使用TextBox和RichTextbox时.
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)保存在我正在使用的项目文件中.
我该怎么做呢
请帮我...
我试图Button在XAML 中将两个字符串连接为内容.但它不起作用.我尝试过如下:
<Button Content=""String1"+{DynamicResource String2}"/>
Run Code Online (Sandbox Code Playgroud) <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).但是我想通过它的行或列获取网格子节点.我该怎么办呢.
提前致谢.

现在光标聚焦在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) <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中通过XAML将大写转换为小写,如下所示:
<TextBox Height="86" CharacterCasing="Upper"/>
Run Code Online (Sandbox Code Playgroud)
我想实现用同样的场景TextBlock,Label和Button.
我该怎么做?
<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 。可以这样创建吗?
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]" 不匹配的字符串.