如何在后面的代码中设置TextBlock.TextProperty [英] How can I set TextBlock.TextProperty in code behind

查看:323
本文介绍了如何在后面的代码中设置TextBlock.TextProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everybody,

我正在尝试使用codebehind在chartToolKit中创建线图。

我需要做的最后一件事就是以某种方式连接textblock.textproperty和

TemplateBinding FormattedDependentValue。



Colud有人帮我这个,非常请:)?我用Google搜索了一整天。



ControlTemplate ct = new ControlTemplate(typeof(LineDataPoint));

var grid1 = new FrameworkElementFactory( typeof(Grid));

grid1.SetValue(Grid.WidthProperty,130.0);

grid1.SetValue(Grid.HeightProperty,130.0);

grid1.SetValue(Grid.BackgroundProperty,new SolidColorBrush(Colors.White));

grid1.SetValue(Grid.Horizo​​ntalAlignmentProperty,Horizo​​ntalAlignment.Center);

grid1。 SetValue(Grid.VerticalAlignmentProperty,VerticalAlignment.Bottom);

var textBlock1 = new FrameworkElementFactory(typeof(TextBlock)); textBlock1.SetValue(TextBlock.WidthProperty,130.0);

textBlock1.SetValue(TextBlock.HeightProperty,130.0);



textBlock1.SetValue( TextBlock.BackgroundProperty,新的SolidColorBrush(Colors.Black));

textBlock1.SetValue(TextBlock.ForegroundProperty,new SolidColorBrush(Colors.Yellow));

textBlock1.SetValue(TextBlock.TextProperty,?????? ?????????????????????????????????? b
grid1.AppendChild(textBlock1);

ct.VisualTree = grid1;

Setter st6 = new Setter(LineDataPoint.TemplateProperty,ct);

解决方案

< blockquote>我不敢相信你真的需要更改 TextBlock.TextProperty ,我怀疑你需要更改 TextBlock.Text 属性。你能看到这两个属性之间的显着差异吗? :-)



最有可能的是,您需要了解依赖属性的工作原理。粗略地说,它们被发明用于XAML绑定。 DependencyProperty 的实例静态注册了常规属性的声明类型(使用属性存储来存储其值,而不是其声明类的实例)和一些唯一的名称由XAML识别,允许绑定 PropertyChangesCallback 和其他有用的东西。请参阅:

http:// msdn .microsoft.com / zh-CN / library / ms752914%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dn169387%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en -us / magazine / cc794276.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.dependencypr operty%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ms753358%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/ en-us / library / ms752375%28v = vs.110%29.aspx [ ^ ]。



-SA


这解决了我的问题:



  var  binding =  new 绑定(); 
binding.Path = new PropertyPath( );
textBlock1.SetBinding(TextBlock.TextProperty,binding);


Hello Everybody,
I'm trying to create linechart in chartToolKit using codebehind.
Last thing I need to do is connect somehow textblock.textproperty with
TemplateBinding FormattedDependentValue.

Colud someone help me with this, very please:)? I googled for this for whole day.

ControlTemplate ct = new ControlTemplate(typeof(LineDataPoint));
var grid1 = new FrameworkElementFactory(typeof(Grid));
grid1.SetValue(Grid.WidthProperty, 130.0);
grid1.SetValue(Grid.HeightProperty, 130.0);
grid1.SetValue(Grid.BackgroundProperty,new SolidColorBrush(Colors.White));
grid1.SetValue(Grid.HorizontalAlignmentProperty, HorizontalAlignment.Center);
grid1.SetValue(Grid.VerticalAlignmentProperty, VerticalAlignment.Bottom);
var textBlock1 = new FrameworkElementFactory(typeof(TextBlock)); textBlock1.SetValue(TextBlock.WidthProperty, 130.0);
textBlock1.SetValue(TextBlock.HeightProperty, 130.0);

textBlock1.SetValue(TextBlock.BackgroundProperty, new SolidColorBrush(Colors.Black));
textBlock1.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.Yellow));
textBlock1.SetValue(TextBlock.TextProperty,??????????????????????????? );

grid1.AppendChild(textBlock1);
ct.VisualTree = grid1;
Setter st6 = new Setter(LineDataPoint.TemplateProperty, ct);

解决方案

I cannot believe you really need to change TextBlock.TextProperty, I suspect you need to change TextBlock.Text property. Can you see the striking difference between those two properties? :-)

Most likely, you need to understand how dependency properties work. Roughly speaking, they have been invented to be used in XAML binding. An instance of DependencyProperty is statically registered with the declaring type of a regular property (using property store for storing its value, not an instance of its declaring class) and some unique name to by recognizable by XAML, which allows to bind PropertyChangesCallback and other useful things. Please see:
http://msdn.microsoft.com/en-us/library/ms752914%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dn169387%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/magazine/cc794276.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms753358%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms752375%28v=vs.110%29.aspx[^].

—SA


This has sorted out my problem:

var binding = new Binding();
                binding.Path = new PropertyPath("Value");
                textBlock1.SetBinding(TextBlock.TextProperty, binding);


这篇关于如何在后面的代码中设置TextBlock.TextProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆