动态创建 超级按钮

feng199 2010-06-02 04:27:18

<HyperlinkButton x:Name="HyperLink1"
Content="{Binding Path=ApplicationStrings.AboutPageTitle, Source={StaticResource ResourceWrapper}}"/>

以下是动态创建,可是在Content 该怎么赋值?

HyperlinkButton Hb = new HyperlinkButton();
Hb.Name = "HyperLink1"
Hb.Content = ??????????????这里该怎么赋值
...全文
114 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
feng199 2010-06-03
  • 打赏
  • 举报
回复
太感谢您了!再指点一下,还有两个地方赋值也出错

<HyperlinkButton x:Name="HyperLink1"
NavigateUri="/About" TargetName="ContentFrame"/>

c#代码:
HyperlinkButton Hb = new HyperlinkButton();
hb.NavigateUri = new System.Uri("http://localhost:19796/About");//这里该怎么赋值?
hb.TargetName = "ContentFrame";//这里该怎么赋值?
Sunpire 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 feng199 的回复:]
引用 2 楼 sunpire 的回复:
LZ这里使用的只是绑定给StaticResource ,这是最简单的,可以不使用绑定,直接赋值就可以了,因为ResourceWrapper这种东西存放的只是静态字符串,不会实现 INotifyPropertyChanged 接口。

Hb.Content = ResourceWrapper.ApplicationStrings.AboutPageTit……
[/Quote]

ResourceWrapper 只是我从你给出的代码中得到资源的名称,你又没有给出ResourceWrapper中的具体代码,怎么能照抄呢,我只是给你指一个方向而已?

不过根据楼主的“ResourceWrapper”、“ApplicationStrings”这两个名字 ,应是 VS2010 自动生成的,ResourceWrapper 是一个资源包装器class,专门用于包装静态资源,LZ 直接跨过 ResourceWrapper调用相应的静态资源就可以:

Hb.Content = ApplicationStrings.AboutPageTitle ;
feng199 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sunpire 的回复:]
LZ这里使用的只是绑定给StaticResource ,这是最简单的,可以不使用绑定,直接赋值就可以了,因为ResourceWrapper这种东西存放的只是静态字符串,不会实现 INotifyPropertyChanged 接口。

Hb.Content = ResourceWrapper.ApplicationStrings.AboutPageTitle ;
[/Quote]

为什么会提示:
An object reference is required for the non-static field, method,
or property 'BusinessApplication1.ResourceWrapper.ApplicationStrings.get'
blackant2 2010-06-03
  • 打赏
  • 举报
回复
sl区的人都好穷
-- 顶 sunpire
Sunpire 2010-06-03
  • 打赏
  • 举报
回复

hb.Content = ApplicationStrings.AboutPageTitle ;
hb.NavigateUri = new System.Uri("/About" , System.UriKind.Relative);
hb.TargetName = "ContentFrame";


基于下面的 UriMapping

<navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}"
Source="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed">
<navigation:Frame.UriMapper>
<uriMapper:UriMapper>
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
</uriMapper:UriMapper>
</navigation:Frame.UriMapper>
</navigation:Frame>


给LZ的建议是一步一步学习,先掌握一些基本的Silverlight的知识。 看帮助文档是很有效的。
Silverlight 3 的在线帮助
http://msdn.microsoft.com/zh-cn/library/cc838158(v=VS.95).aspx
feng199 2010-06-03
  • 打赏
  • 举报
回复
hb.NavigateUri = new System.Uri("http://localhost:19796/Views/About");
能否赋值 相对路径?比如:“/About”

hb.TargetName = "ContentFrame";
暂时先改为 "_self" 或 "_blank" 都不行,最好能直接绑定 ContentFrame

feng199 2010-06-03
  • 打赏
  • 举报
回复
hb.NavigateUri = new System.Uri("http://localhost:19796/Views/About");
hb.TargetName = "_bank";

这样也不行
Sunpire 2010-06-02
  • 打赏
  • 举报
回复
对于 Source 为 StaticResource 的同样也可以这样写 Binding,不过 Mode 一般就不会是 TwoWay 了,而是 OneTime ,好像记得是 TwoWay 的Binding 一定得要实现INotifyPropertyChanged接口。
Sunpire 2010-06-02
  • 打赏
  • 举报
回复
如果是绑定至某个实体的实例,则可以用

System.Windows.Data.Binding binding = new System.Windows.Data.Binding()
{
Source = 要被绑定的实体的实例,
Path = new PropertyPath("属性路径"),
Mode = System.Windows.Data.BindingMode.TwoWay,
Converter = xxxx
};
this.Hb.SetBinding(HyperlinkButton.ContentProperty, binding);
Sunpire 2010-06-02
  • 打赏
  • 举报
回复
LZ这里使用的只是绑定给StaticResource ,这是最简单的,可以不使用绑定,直接赋值就可以了,因为ResourceWrapper这种东西存放的只是静态字符串,不会实现 INotifyPropertyChanged 接口。

Hb.Content = ResourceWrapper.ApplicationStrings.AboutPageTitle ;
feng199 2010-06-02
  • 打赏
  • 举报
回复
晕,我的帖子怎么还没出来啊,
管理员,把我放出来吧

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧