我的WPF应用程序中有一个TextBox.我已经为验证错误定义了一个ControlTemplate,如下所示:
<ControlTemplate x:Key="validationTemplate">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Bottom" Text="Invalid Input: "></TextBlock>
<AdornedElementPlaceholder />
</DockPanel>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
我的TextBox如下:
<TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}">
<TextBox.Text>
<Binding Path="TEXT1" ValidatesOnDataErrors="True" validatesOnExceptions="True">
</Binding>
</TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
现在,如果我的TextBox添加了ValidationRule,然后我在那里验证,错误模板正确应用.但由于其他一些问题我无法做到这一点.
所以我必须在PreviewLostKeyboardFocus中验证TextBox的内容.我正在验证TextBox.现在我想在后面的代码中为TextBox设置错误模板,但我无法做到!!
我尝试了这个,但它并不像有意的那样工作::
private void blockTextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
TextBox txtBox = sender as TextBox;
txtBox.Template = this.FindResource("validationTemplate") as ControlTemplate;
//this behaves strange; it removes the TextBox and places the ErrorTemplate.
//I want it to behave like the way WPF does internally wherein it places
//the error template around TExtBox
}
Run Code Online (Sandbox Code Playgroud)
问题1:我想知道如何将错误模板添加到TextBox
问题2:我想知道如何从代码中设置控件模板的错误消息.例如,我想将默认错误消息"Invalid …
我的代码如下:
void mainDataContextObj_CutSelectedColumnEvent(string columnId)
{
IList<DataGridColumn> columns = dg.Columns;
for(int i=2; i<dg.Columns.Count; i++)
{
DataGridColumnHeader headerObj = dg.Columns[i].Header as DataGridColumnHeader;
//This always returns headerObj as null!!!
}
}
Run Code Online (Sandbox Code Playgroud)
我需要DataGridColumnHeader从专栏.我哪里错了?
我想在WPF中单击一个Button时添加一个弹出窗口.我不想在我的XAML中添加Popup代码.它必须是代码背后的代码.
我的XAML如下::
<Window x:Class="Test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Button x:Name="button1" Click="button1_Click">Button</Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
我的XAML文件有一个简单的按钮.单击按钮,我试图执行以下代码.
private void button1_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
Popup codePopup = new Popup();
TextBlock popupText = new TextBlock();
popupText.Text = "Popup Text";
popupText.Background = Brushes.LightBlue;
popupText.Foreground = Brushes.Blue;
codePopup.Child = popupText;
codePopup.PlacementTarget = button;
codePopup.IsOpen = true;
}
Run Code Online (Sandbox Code Playgroud)
但是为什么弹出窗口没有附加到窗口.我的意思是即使我切换窗口也会显示弹出窗口.另外,当我调整窗口大小时,Popup不再放在按钮附近?
我有一个绑定到某个属性的TextBox.我已经实现了IDataErrorInfo来执行验证.最近我在网上看到了一些控制,显示出像红色三角形一样的错误.我附上了以下示例:

我知道我必须编写错误模板,以便在发生错误时显示它.当用户悬停红色三角形时,它将在工具提示中显示错误消息.如何显示错误文本框,就像我上传的文本框一样.如何在错误模板中获取红色三角形?
我已经完成了WPF项目?我想知道哪些是优化我的代码的可能技术,尤其是XAML和数据绑定.
技巧有效的数据绑定.
请注意我使用的是MVVM架构.
我知道这个问题已经在许多网站和 StackOverFlow 中以不同的方式被问过很多次,但我找到的所有答案都没有帮助我,准确地说,我无法理解它们并在我的应用程序中实现。所以我想从我的应用程序中添加一些代码,以便你们可以更好地帮助我。
问题陈述:我正在使用 WPF DataGrid。我添加了一个上下文菜单,有 3 个选项“剪切”、“复制”、“粘贴”。我正在使用 MVVM 进行开发。我想将这些选项数据绑定到我的 ViewModel 中的命令。但我却无法做到同样的事情。上下文菜单选项根本没有绑定数据!
这是我的 XAML 网格代码:
<custom:DataGrid
x:Name="DataGrid_Standard"
Grid.Row="1" Grid.Column="1"
AutoGenerateColumns="False"
IsSynchronizedWithCurrentItem="True"
Background="Transparent"
ItemsSource="{Binding FullGridData}"
ItemContainerStyle="{StaticResource defaultRowStyle}"
ColumnHeaderStyle="{StaticResource DefaultColumnHeaderStyle}"
Grid.ColumnSpan="2">
Run Code Online (Sandbox Code Playgroud)
然后我有一个 ContextMenu 和一个标题元素的样式
<ContextMenu x:Key="columnHeaderMenu">
<MenuItem Command="{Binding CutCommand}"
Header="Test" />
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
</ContextMenu>
<Style TargetType="{x:Type custom:DataGridColumnHeader}" x:Key="DefaultColumnHeaderStyle">
<Setter Property="ContextMenu" Value="{DynamicResource columnHeaderMenu}" >
</Style>
Run Code Online (Sandbox Code Playgroud)
这行代码出现在我的构造函数中
public Window1()
{
this.DataContext = new AppData();
}
Run Code Online (Sandbox Code Playgroud)
此代码位于我的 AppData 类中:
public class AppData
{
private IList<GridData> fullGridData = new ObservableCollection<GridData>();<br>
public IList<GridData> …Run Code Online (Sandbox Code Playgroud) 我正在经历这个问题 C#,Regex.Match整个单词
它说匹配整个单词使用"\ bpattern\b" 这适用于匹配整个单词而没有任何特殊字符,因为它仅用于单词字符!
我需要一个表达式来匹配带有特殊字符的单词.我的代码如下
class Program
{
static void Main(string[] args)
{
string str = Regex.Escape("Hi temp% dkfsfdf hi");
string pattern = Regex.Escape("temp%");
var matches = Regex.Matches(str, "\\b" + pattern + "\\b" , RegexOptions.IgnoreCase);
int count = matches.Count;
}
}
Run Code Online (Sandbox Code Playgroud)
但由于%,它失败了.我们有解决方法吗? 可以有其他特殊字符,如'space','(',')'等
我只是在进行一些验证并且坚持这个但是:(我想要一个仅包含[az] [AZ] [0-9] [_]的文本.
它应该以任何顺序接受任何上述字符.所有其他字符都将文本标记为无效.
我试过这个,但它没有用!
{
......
Regex strPattern = new Regex("[0-9]*[A-Z]*[a-z]*[_]*");
if (!strPattern.IsMatch(val))
{
return false;
}
return true
}
Run Code Online (Sandbox Code Playgroud) 我正在使用一个ADO .Net Entity Model查询MySQL数据库.我对它的实现和使用感到非常高兴.我决定看看如果我查询了100万条记录会发生什么,并且它有严重的性能问题,我不明白为什么.
系统挂起一段时间,然后我得到
我的代码如下::
try
{
// works very fast
var data = from employees in dataContext.employee_table
.Include("employee_type")
.Include("employee_status")
orderby employees.EMPLOYEE_ID descending
select employees;
// This hangs the system and causes some deadlock exception
IList<employee_table> result = data.ToList<employee_table>();
return result;
}
catch (Exception ex)
{
throw new MyException("Error in fetching all employees", ex);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么ToList()花了这么长时间?
另外,如何避免此异常以及查询百万条记录的理想方式是什么?
我一直在使用 WPF 处理所有隧道和冒泡事件,我必须说它们非常强大。
但我总是问自己是使用 [ PreviewKeyUp 和 KeyUp ] 还是 [ PreviewKeyDown 和 Keydown ]?
我应该使用哪种组合来对按键做出反应,为什么?
wpf ×7
c# ×3
.net ×2
code-behind ×2
mvvm ×2
regex ×2
validation ×2
ado.net ×1
binding ×1
button ×1
command ×1
contextmenu ×1
data-binding ×1
datagrid ×1
escaping ×1
forms ×1
header ×1
keydown ×1
keyup ×1
mysql ×1
optimization ×1
popup ×1
templates ×1
textbox ×1
windows ×1