gpt4 book ai didi

silverlight - Silverlight通过CustomControl后端代码按键访问资源

转载 作者:行者123 更新时间:2023-12-03 10:29:54 25 4
gpt4 key购买 nike

设想:

我有一些DataTemplate资源的 View

<DataTemplate x:Key="myDragCueTemplate">
<Border Background="Blue"
Opacity="0.5"
Width="250">
<TextBlock Text="{Binding}" HorizontalAlignment="Left"></TextBlock>
</Border>
</DataTemplate>

我有一个从ListBox派生的自定义控件。在某个事件的自定义列表框中,我想从View的资源中获取数据模板。
public class MyListBox : ListBox
{
public MyListBox()
{
this.DefaultStyleKey = typeof(MyListBox);
}
...

itemDragCue.ContentTemplate = this.Resources["myDragCueTemplate"] as DataTemplate;

...

我尝试将数据模板添加到单独的.xaml文件中,并添加了ResourceDictionary,但是它仍然没有选择。

如何在自定义控件的后端中获取资源?

谢谢。

最佳答案

this.Resources将仅提供在中声明的资源

<UserControl x:Class="MyListbox">
<UserControl.Resources>

我建议将myDragCueTemplate放在ResourceDictionary中。然后,您将必须在后面的代码中阅读该ResourceDictionary,并提取所需的特定资源。

试试这个
const string resourcesPath = "/AssemblyName;component/Resources.xaml";
Uri resourceUri = new Uri(resourcesPath, UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(resourceUri);
StreamReader sr = new StreamReader(sri.Stream);
ResourceDictionary dictionary = (ResourceDictionary) XamlReader.Load(sr.ReadToEnd());
itemDragCue.ContentTemplate = dictionary["myDragCueTemplate"] as DataTemplate;

关于silverlight - Silverlight通过CustomControl后端代码按键访问资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9999968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com