小编Pom*_*oma的帖子

如何在ControlTemplate中声明事件处理程序?

我有以下内容ControlTemplate:

<ControlTemplate>
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="400">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="18" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="45" />
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="1" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Number" />
        <TextBox Grid.Column="2" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Login" />
        <TextBox Grid.Column="3" Template="{StaticResource watermark}" HorizontalAlignment="Stretch" Margin="4,0,0,4" Tag="Password" />
        <Button Grid.Column="4" HorizontalAlignment="Stretch" Content="Add" Margin="4,0,0,4" Click="AddUser_Click"/>
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

我该怎么写AddUser_Click才能访问文本框Text属性?

upd:只是为了说清楚.我知道如何在Click这里连接事件处理程序.问题是如何阅读其中的文本框内容,因为我不能给它们起名,因为它们在模板中.

c# wpf

6
推荐指数
3
解决办法
9478
查看次数

如何在XAML中创建类的实例?

我想创建没有可视元素的简单实用程序类,并在XAML中创建它,以便我可以定义数据绑定.我试图创建派生的类DependencyObject并在Window.Resources节中创建它,但它不会调用任何构造函数.

.net c# wpf xaml

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

WPF中的条件样式

我做了一个TextBlock看起来像链接的风格:

<Style x:Key="linkStyle" TargetType="TextBlock">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="TextDecorations" Value="Underline" />
        </Trigger>
    </Style.Triggers>
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Foreground" Value="Blue" />
    <EventSetter Event="MouseLeftButtonDown" Handler="navigateLink" />
</Style>
Run Code Online (Sandbox Code Playgroud)

如何在TextBlock.Texthttp://开头时应用它?

.net c# wpf xaml

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

如何在属性参数中进行方法引用

我想创建一个属性,允许我指定一些应用于属性的方法,如下所示:

public class MyClass
{
    [MyAttribute(Converter="ConverterMethod")]
    public string Prop { get; set; }

    public static string ConverterMethod(string src)
    {
        return src + " converted";
    }
}
Run Code Online (Sandbox Code Playgroud)

这样做的"正确"方法是什么?

以下是我看到的方式:

  1. 在运行时使用反射创建字符串属性并提取相应的方法
  2. Dictionary<string, Func<string, string>>在运行时使用相应的方法制作并填充它.然后使用属性的字符串属性作为键提取方法.这种方法更能抵抗重构,如果我重命名方法一切都会起作用(字典键将保持不变)
  3. 制作'IConverter'界面并传递typeof(ConverterImpl)给属性.然后在运行时创建转换器实例并使用其接口转换值.这种方式对我来说似乎是最好的,但我从未Type在属性中使用过属性,甚至不知道它们是否序列化良好.

哪种方法最好?还有其他方法吗?人们通常如何做这样的事情?

.net c# reflection

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

如何获取另一个线程的ThreadStatic值?

是否有可能给定Thread参考来获取ThreadStatic该线程的值?

.net c# multithreading

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

简单的角度应用程序不起作用

我正在尝试创建基本的角度应用程序,它会抛出错误

<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
    <script type="text/javascript">
        var angularApp = angular.module('angularApp', []);
        angularApp.controller('Ctrl', function($scope) {});
    </script>
</head>
<body>
    <div ng-app ng-controller="Ctrl"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

错误:

Error: [ng:areq] http://errors.angularjs.org/1.4.3/ng/areq?p0=Ctrl&p1=not%20a%20function%2C%20got%20undefined
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:6:416
    at Sb (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:22:18)
    at Qa (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:22:105)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:79:497
    at x (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:59:501)
    at S (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:60:341)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:54:384)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:53:444
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js:19:481'
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

javascript angularjs

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

编组到 json 时如何强制 go 将 `[8]byte` 编码为十六进制

默认情况下,字节切片被编组为 Base64 字符串,字节数组按原样转换:

func main() {
    type Foo struct {
        ByteSlice []byte
        ByteArray [6]byte
    }

    foo := Foo {
        ByteSlice: []byte{0, 0, 0, 1, 2, 3},
        ByteArray: [6]byte{0, 0, 0, 1, 2, 3},
    }
    text, _ := json.Marshal(foo)
    fmt.Printf("%s", text)
}
Run Code Online (Sandbox Code Playgroud)

输出:

{"ByteSlice":"AAAAAQID","ByteArray":[0,0,0,1,2,3]}
Run Code Online (Sandbox Code Playgroud)

有没有办法对字节切片使用十六进制字符串转换?

json go

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

Laravel如何管理MySQL变量?

如果我做

DB::statement("SET @foo := 1;");
DB::select("SELECT @foo;");
Run Code Online (Sandbox Code Playgroud)

我得到了预期1的结果.但我无论在哪里都找不到它是否有保证的结果.如果我理解正确,变量是特定于MySQL连接的.因此,由于某种原因,这两个语句可能会在不同的连接上执行,我会得到不同的结果吗?我可以依靠它始终工作吗?

php mysql laravel

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

PerformanceCounter.NextValue 在某些计算机上挂起

我不知道为什么,但许多计算机在执行以下操作时挂起:

void Init()
{
    net1 = new List<PerformanceCounter>();
    net2 = new List<PerformanceCounter>();
    foreach (string instance in new PerformanceCounterCategory("Network Interface").GetInstanceNames())
    {
        net1.Add(new PerformanceCounter("Network Interface", "Bytes Received/sec", instance));
        net2.Add(new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance));
    }
}

//Once in 1 second
void UpdateStats()
{
    Status.Text = "";
    for (int i = 0; i < net1.Count; i++)                    
         Status.Text += string.Format("{0}/{1} Kb/sec;   ", net1[i].NextValue() / 1024, net2[i].NextValue() / 1024);
}
Run Code Online (Sandbox Code Playgroud)

在某些计算上,程序在第一次调用 时完全挂起UpdateStats(),而其他计算则经历 100% CPU 负载,但程序运行(缓慢)。其他计数器似乎new PerformanceCounter("Processor", "% Processor Time", "_Total")工作正常。

有什么建议吗?这是为什么?

c# system.diagnostics

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

如何对CollectionViewSource的组进行排序

我有以下集合视图

<CollectionViewSource x:Key="messages" Source="{Binding src}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="Group"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
Run Code Online (Sandbox Code Playgroud)

然后我将它分配给TreeView的ItemsSource.现在,如何按名称对群组进行排序?他们似乎有随机顺序.

.net c# wpf xaml

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