小编M. *_*ley的帖子

简单的WPF IValueConverter和DataTrigger不能一起工作

我一直在使用带数据触发器的值转换器时遇到问题.在我的一些代码,它似乎像DataTriggerPath被应用到根元素,而不是风格适用于哪些元素.

我创建了一个简单的测试用例,我不明白它的行为.我希望它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)

wpf datatrigger ivalueconverter

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

在WPF中取消验证错误时的TextBox输入

我试图弄清楚如何在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)

我有一个错误处理程序...我能用它做什么吗? …

validation wpf textbox

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

从MSBuild中的文件中读取单个值

我正在尝试从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(&quot;@(VersionNumber)&quot;)]" />
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

错误MSB4012:表达式"[assembly:AssemblyVersion("@(VersionNumber)")]"不能在此上下文中使用.项目列表不能与期望项目列表的其他字符串连接.使用分号分隔多个项目列表

msbuild version

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

什么时候应该解雇"财产变更"事件?

当财产的实际价值没有变化时触发"财产已更改"事件是否合适?

public int SomeProperty
{
    get { return this.mSomeProperty; }
    set
    {
        this.mSomeProperty = value;
        OnPropertyChanged(new PropertyChangedEventArgs("SomeProperty"));
    }
}
Run Code Online (Sandbox Code Playgroud)

即使新值与旧值相同,这也会触发事件.这是不好的做法吗?

c# events

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

何时解决托管堆碎片问题

我正在阅读Josh Smith 撰写的博客文章,他使用缓存机制来"减少托管堆碎片".他的缓存减少了创建的短期对象的数量,但代价是执行速度稍慢.

在像C#这样的托管语言中管理堆碎片有多少问题?你如何诊断它是否是一个问题?在什么情况下你通常需要解决它?

.net caching heap-fragmentation

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

C#DLL可以被截去?

问题:是否可以编写可以被限制的C#dll?

我想为WinAPI调用WritePrivateProfileString等编写一个替换库来读取和写入ini文件.

是否可以导出一个函数,例如'WritePrivateProfileString',在C#中实现,这样可以用DllImport对dll进行pinvoke?

我想用托管dll替换本机dll,因此托管dll会被pinvoke调用(而不是本机dll,当托管dll在bin目录中时)调用.

.net c# pinvoke dllimport

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

p4同步到备用位置

我正在编写一个构建脚本,它获取特定更改列表的所有源代码并构建它.我希望能够随时运行脚本,而无需搁置本地更改或将文件移动到临时位置.该脚本将由定义了自己的工作空间的其他人使用.

我认为将Perforce中的所有源代码放在临时位置并从那里构建是最容易的.不幸的是P4同步似乎并不支持这一点,它只能将文件转换成由工作区指定的,这意味着它会覆盖本地更改之前,我可以将文件复制到临时位置的客户视图.

有没有办法p4将文件从Perforce复制到任意位置?

perforce

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

使用C#中的弱处理程序引发PropertyChanged事件时发生ExecutionEngineException

我正在尝试引发一个由弱事件处理程序(通过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

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

从 C 中的 typedef 中减去一个值?

我正在阅读一些 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 中减去一个值是什么意思?

c winapi

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

重用TreeView在WPF中展开[+]并折叠[ - ]按钮

有没有办法重用WPF中节点旁边出现的简单展开[+]和折叠[-]按钮TreeView?我想在我的应用程序的其他地方有一个类似的图形来扩展和折叠一些控件.

wpf treeview button reusability togglebutton

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