【发布时间】:2017-07-01 11:06:24
【问题描述】:
问这个问题的另一种方式是说:
“如何将对象的值设置为24,以便将其作为参数传递给SetValue() 的value 参数?”
明确一点:我只是想在后面的代码中设置依赖属性的值
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Foo {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
TextBlock1.Text = "bar";
TextBlock1.SetValue(FontSizeProperty, 24);
}
}
}
当我构建应用程序时,它成功了!
但是当我调试时,它会抛出一个参数异常,如下所示:
为什么会出现此错误和/或如何解决?
【问题讨论】:
-
为什么有人会否决我的问题,然后至少没有礼貌发表评论?
-
你为什么不直接打电话给
TextBlock1.FontSize = 24;? -
@Clemens - 因为那是 .NET 代码。我想要 XAML 依赖属性,因为它们为应用程序提供了更多功能。
-
这毫无意义。
FontSize是 FontSize 依赖属性的 CLR 包装器。它的 setter 直接调用SetValue(FontSizeProperty, value)。因此,使用TextBlock1.FontSize = 24,您可以直接设置依赖属性。 -
@Clemens - 我不知道。我只是按照我被告知的去做。 :D 此信息是截至 2016 年 10 月的最新信息。
标签: c# wpf xaml dependency-properties