我一直在使用带数据触发器的值转换器时遇到问题.在我的一些代码,它似乎像DataTrigger的Path被应用到根元素,而不是风格适用于哪些元素.
我创建了一个简单的测试用例,我不明白它的行为.我希望它Button会变成红色或蓝色,具体取决于输入到DataTrigger转换器的值,但它Button根本没有受到影响!
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SimpleWpfApplication"
x:Class="SimpleWpfApplication.SimpleUserControl"
ToolTip="UserControl ToolTip">
<UserControl.Resources>
<local:SimpleConverter x:Key="SimpleConverter" />
</UserControl.Resources>
<Button ToolTip="Button ToolTip">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=ToolTip, Converter={StaticResource SimpleConverter}}"
Value="Button ToolTip">
<Setter Property="Background" Value="Red" />
</DataTrigger>
<DataTrigger
Binding="{Binding Path=ToolTip, Converter={StaticResource SimpleConverter}}"
Value="UserControl ToolTip">
<Setter Property="Background" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
一个简单的转换器:
class SimpleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value; …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何在TextBox发生验证错误时取消用户输入.如果用户试图输入无效字符,我想阻止它被添加到TextBox.
如何添加或修改下面的代码以防止TextBox接受无效字符?没有听过这个TextBox.TextChanged活动有可能吗?
我TextBox看起来像:
<TextBox Validation.Error="OnSomeTextBoxValidationError">
<TextBox.Text>
<Binding Path="Value" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:SomeValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
我的自定义验证规则如下所示:
public class SomeValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string hex_string = value as string;
Match invalid_chars = Regex.Match(hex_string, "[^0-9a-fA-F]");
bool is_valid = (invalid_chars.Success == false);
string error_context = null;
if (is_valid == false)
{
error_context = "Invalid characters";
}
return new ValidationResult(is_valid, error_context);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个错误处理程序...我能用它做什么吗? …
我正在尝试从MSBuild中的文件中读取版本号:
<ItemGroup>
<VersionFile Include="Properties\VERSION" />
</ItemGroup>
<Target Name="BeforeBuild">
<ReadLinesFromFile File="@(VersionFile)">
<Output TaskParameter="Lines" ItemName="VersionNumber" />
</ReadLinesFromFile>
</Target>
Run Code Online (Sandbox Code Playgroud)
我只需要这个文件的第一行.如何将该值与另一个字符串连接起来WriteLinesToFile?这不起作用:
<WriteLinesToFile
File="$(AssemblyVersionFile)"
Lines="[assembly: AssemblyVersion("@(VersionNumber)")]" />
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
错误MSB4012:表达式"[assembly:AssemblyVersion("@(VersionNumber)")]"不能在此上下文中使用.项目列表不能与期望项目列表的其他字符串连接.使用分号分隔多个项目列表
当财产的实际价值没有变化时触发"财产已更改"事件是否合适?
public int SomeProperty
{
get { return this.mSomeProperty; }
set
{
this.mSomeProperty = value;
OnPropertyChanged(new PropertyChangedEventArgs("SomeProperty"));
}
}
Run Code Online (Sandbox Code Playgroud)
即使新值与旧值相同,这也会触发事件.这是不好的做法吗?
我正在阅读Josh Smith 撰写的博客文章,他使用缓存机制来"减少托管堆碎片".他的缓存减少了创建的短期对象的数量,但代价是执行速度稍慢.
在像C#这样的托管语言中管理堆碎片有多少问题?你如何诊断它是否是一个问题?在什么情况下你通常需要解决它?
问题:是否可以编写可以被限制的C#dll?
我想为WinAPI调用WritePrivateProfileString等编写一个替换库来读取和写入ini文件.
是否可以导出一个函数,例如'WritePrivateProfileString',在C#中实现,这样可以用DllImport对dll进行pinvoke?
我想用托管dll替换本机dll,因此托管dll会被pinvoke调用(而不是本机dll,当托管dll在bin目录中时)调用.
我正在编写一个构建脚本,它获取特定更改列表的所有源代码并构建它.我希望能够随时运行脚本,而无需搁置本地更改或将文件移动到临时位置.该脚本将由定义了自己的工作空间的其他人使用.
我认为将Perforce中的所有源代码放在临时位置并从那里构建是最容易的.不幸的是P4同步似乎并不支持这一点,它只能将文件转换成由工作区指定的,这意味着它会覆盖本地更改之前,我可以将文件复制到临时位置的客户视图.
有没有办法p4将文件从Perforce复制到任意位置?
我正在尝试引发一个由弱事件处理程序(通过PropertyChangedEventManager)监听的PropertyChanged事件.出于某种原因,我在举起事件时遇到了ExecutionEngineException.
我的事件提升代码如下:
protected virtual void RaisePropertyChanged(string aPropertyName)
{
var lHandler = this.PropertyChanged;
if (lHandler != null)
{
// ExecutionEngineException is thrown here
lHandler(this, new PropertyChangedEventArgs(aPropertyName));
}
return;
}
Run Code Online (Sandbox Code Playgroud)
我的处理代码如下:
public bool ReceiveWeakEvent(Type aManagerType, object aSender, EventArgs e)
{
bool lHandled = false;
if (aManagerType == typeof(PropertyChangedEventManager))
{
OnPropertyChanged(aSender, e as PropertyChangedEventArgs);
}
return lHandled;
}
Run Code Online (Sandbox Code Playgroud)
当我搜索此异常时,我没有得到任何有用的结果,并且异常本身不包含任何有用的信息!是什么导致了这个问题?
c# weak-references inotifypropertychanged executionengineexception
我正在阅读一些 Windows API 头文件,我看到了一些我不明白的代码:
typedef void *HANDLE;
typedef HANDLE DPI_AWARENESS_CONTEXT;
#define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
Run Code Online (Sandbox Code Playgroud)
从void *typedef 中减去一个值是什么意思?
有没有办法重用WPF中节点旁边出现的简单展开[+]和折叠[-]按钮TreeView?我想在我的应用程序的其他地方有一个类似的图形来扩展和折叠一些控件.
c# ×3
wpf ×3
.net ×2
button ×1
c ×1
caching ×1
datatrigger ×1
dllimport ×1
events ×1
msbuild ×1
perforce ×1
pinvoke ×1
reusability ×1
textbox ×1
togglebutton ×1
treeview ×1
validation ×1
version ×1
winapi ×1