我正在尝试弄清楚如何在运行时以编程方式在Silverlight 4应用程序中应用主题.我认为这应该像从XAML加载资源字典并将其与应用程序的合并字典合并一样简单.到目前为止,这是我的代码:
var themeUri = new Uri(
"OurApp;component/Themes/Classic/Theme.xaml", UriKind.Relative);
var resourceInfo = GetResourceStream(themeUri);
using (var stream = resourceInfo.Stream)
{
using (var reader = new StreamReader(stream))
{
var xamlText = reader.ReadToEnd();
var dict = XamlReader.Load(xamlText) as ResourceDictionary;
Resources.MergedDictionaries.Add(dict);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的XamlParseException是,在致电期间提出了XamlReader.Load:
在'Bar'类型中找不到可附加属性'Foo'.
这种连接正确是正确声明,并在XAML命名空间声明正确引用所需的命名空间.如果通过App.xaml标记以声明方式加载到合并的字典中,附加属性XAML可以正常工作.
这是我正在尝试在运行时加载的XAML的缩写副本:
<ResourceDictionary xmlns:u="clr-namespace:Company.Product.Utils"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ControlPanelStyle" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid Margin="0" u:Bar.Foo="True">
<!-- Stuff and things -->
<ContentPresenter Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
为什么在运行时加载XAML时附加属性的引用在"静态"加载时工作得很好?
silverlight xaml resourcedictionary attached-properties silverlight-4.0
是否可以直接在双向绑定后面设置值,而不知道绑定属性?
我有一个附加属性绑定到这样的属性:
<Element my:Utils.MyProperty="{Binding Something}" />
Run Code Online (Sandbox Code Playgroud)
现在我想Something从附加属性的角度更改有效存储的值.所以我不能直接访问绑定属性,而只能引用DependencyObject(即Element实例)和DependencyProperty对象本身.
简单地设置它的问题DependencyObject.SetValue是这有效地删除了绑定,但我想更改底层绑定属性.
使用BindingOperations我可以得到Binding和BindingExpression.现在有办法访问它背后的属性并改变它的价值吗?
我想为附加的Property添加每个Codebehind的DataBinding,并希望Canvas.Left在TextBox中显示该属性.如何添加此属性?
我的目标是通过DependencyProperties操作我的应用程序的文本样式.我得到了一个图表,其中的文本将被大小,字体家族,颜色等操纵.所以我想使用类似于Word等富文本编辑器的界面.
我在我的TextStyleVM http://shevaspace.blogspot.com/2006/12/i-have-some-fun-with-formattedtext_14.html中使用此代码
所以我有一个FontFamilyProperty和一个Getter和Setter:
public static DependencyProperty FontFamilyProperty =
DependencyProperty.Register(
"FontFamily",
typeof(FontFamily),
typeof(OutlinedText),
new FrameworkPropertyMetadata(
SystemFonts.MessageFontFamily,
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsMeasure),
new ValidateValueCallback(IsValidFontFamily));
public FontFamily FontFamily
{
get { return (FontFamily)base.GetValue(FontFamilyProperty); }
set { base.SetValue(FontFamilyProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
然后有一个ToStyle方法,它设置图表标签的样式,这些样式将被操作:
Style style = new Style();
Binding fontFamilyBinding = new Binding("FontFamily");
fontFamilyBinding.Source = this;
Setter fontFamilySetter = new Setter();
fontFamilySetter.Property = TextBlock.FontFamilyProperty;
fontFamilySetter.Value = fontFamilyBinding;
style.Setters.Add(fontFamilySetter);
return style;
Run Code Online (Sandbox Code Playgroud)
现在这适用于TextBox.文本框显示当前的FontFamily,如果我在文本框中输入新的有效FontFamily(如Arial),则会更改标签的FontFamily.
但是,我想要的是一个组合框,它显示SystemFonts,我可以为我的标签选择一个FontFamily.但是,绑定似乎不起作用.既不显示系统字体也不显示标签的当前字体.组合框只是空的.
这是我的xaml:
<r:RibbonLabel Content="FontFamily" />
<!--these do not work-->
<r:RibbonComboBox SelectedItem="{Binding FontFamily}"/>
<r:RibbonComboBox ItemsSource="{Binding …Run Code Online (Sandbox Code Playgroud) 我创建了一个附加行为,用于在Func<bool>调用行为时执行类型委托.以下是依赖属性定义.
public static readonly DependencyProperty SendToDetailBehaviorProperty = DependencyProperty.RegisterAttached("SendToDetailBehavior", typeof(Func<bool>), typeof(ListDetailAspectSendToDetailBehavior), new UIPropertyMetadata(null, SendToDetail));
Run Code Online (Sandbox Code Playgroud)
我有它按预期工作但是在我的XAML中我收到以下错误,阻止设计器加载.
未找到属性'SendToDetailBehavior'或类型'SortableListView'不可序列化
下面你会发现xaml.
<Controls:SortableListView Grid.Row="0"
Grid.Column="0"
Name="lvwLocations"
MinHeight="150"
MinWidth="{Binding Path=BusinessObject.Locations, ValidatesOnDataErrors=true, Converter={StaticResource AlwaysReturn1Converter}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Style="{DynamicResource SortableListViewStyle}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
IsSynchronizedWithCurrentItem="True"
**behaviors:ListDetailAspectSendToDetailBehavior.SendToDetailBehavior="{Binding Path=LocationListDetail.SendFocusToDetail}"**
ItemsSource="{Binding Path=LocationListDetail.MasterList}"
SelectedItem="{Binding Path=LocationListDetail.DetailItem, Mode=TwoWay}"
MouseDoubleClick="lvwLocations_MouseDoubleClick">
Run Code Online (Sandbox Code Playgroud)
例如,如果我将Dependancy属性的基础类型更改bool为,则错误消失.
正如我所说的附加行为正在起作用,只有设计师才会爆炸.我已经找到了关于这方面的文档并且空洞了.我希望有人在这里有一些见解.
谢谢,BDN
wpf dependency-properties attachedbehaviors attached-properties
我有只读附加属性的问题。我是这样定义的:
public class AttachedPropertyHelper : DependencyObject
{
public static readonly DependencyPropertyKey SomethingPropertyKey = DependencyProperty.RegisterAttachedReadOnly("Something", typeof(int), typeof(AttachedPropertyHelper), new PropertyMetadata(0));
public static readonly DependencyProperty SomethingProperty = SomethingPropertyKey.DependencyProperty;
}
Run Code Online (Sandbox Code Playgroud)
我想在 XAML 中使用它:
<Trigger Property="m:AttachedPropertyHelper.Something" Value="0">
<Setter Property="FontSize" Value="20"/>
</Trigger>
Run Code Online (Sandbox Code Playgroud)
但是编译器不想使用它。结果,我有两个错误:
在“ReadonlyAttachedProperty.AttachedPropertyHelper”类型上找不到样式属性“Something”。第 11 行位置 16。
在类型“TextBlock”中找不到属性“Something”。
所有这些魔法让我有点不清楚.据我所知,依赖属性从DependencyObject继承,因此存储了值:
如果未指定值,则从链接获取父元素.
protected object GetValue(string propertyName)
{
if (LocalValues.ContainsKey(propertyName))
{
return LocalValues[propertyName];
}
return Parent.GetValue(propertyName);
}
Run Code Online (Sandbox Code Playgroud)
我这是对的吗?
我也不明白附加属性的值存储在哪里?
Control.FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
typeof(Control), new FrameworkPropertyMetadata(SystemFonts.MessageFontSize,
FrameworkPropertyMetadataOptions.Inherits));
Run Code Online (Sandbox Code Playgroud)
Addached属性上的AddOwner方法调用是否为实例字段赋值?什么时候发生这种情况,价值在哪里?
谢谢!
我试图在可视状态的VisualState.Setters中通过XAML更改控件的RelativePanel附加属性,但属性不会更改,因此我创建了一个依赖项属性,以便通过代码进行测试,两者都没有.
有没有办法刷新到一组新的值,如:
<VisualState.Setters>
<Setter Target="TimestablesControl.RelativePanel.RightOf" Value=""/>
<Setter Target="TimestablesControl.RelativePanel.AlignRightWithPanel" Value="false"/>
<Setter Target="TimestablesControl.RelativePanel.AlignLeftWithPanel" Value="true"/>
</VisualState.Setters>
Run Code Online (Sandbox Code Playgroud)
并使视图更"响应"?
xaml attached-properties win-universal-app windows-10 uwp-xaml
在 Xaml 中,我可以使用 local:TestClass.TestProperty="1" 设置自定义附加属性
我可以使用 {Binding Path=(Namespace:[OwnerType].[PropertyName])} {Binding Path=(local:TestClass.TestProperty)} 绑定到自定义附加属性
但是,当我需要在 SortDescription 中使用自定义附加属性时,如何指定命名空间?我可以使用 new SortDescription("(Grid.Row)", ListSortDirection.Descending) 绑定到附加属性,但在这里我无法在任何地方设置命名空间......
最好的问候,杰斯珀
附加属性应用于对象的顺序是什么?我想我应该忽略这一点,但在我的场景中:我有一个附加属性将VM粘贴到View,然后是另一个依赖于第一个的附加属性.我试图看看如果第二个设置在第一个之前会发生什么,但我无法设法得到错误!即第一个(模型)总是在第二个之前设置,无论xaml中的顺序如何.谁在推动分配令?我可以改变吗?
现在我通过订阅proeprty更改事件来处理迟到的声明:
DependencyPropertyDescriptor dd = DependencyPropertyDescriptor.FromProperty(FrameworkElement.DataContextProperty,depo.GetType());
dd.AddValueChanged(depo, (s, a) =>
{
ChangeDatacontext(s as DependencyObject);
}
Run Code Online (Sandbox Code Playgroud)
为了模拟问题,我手动设置了一个新的datacontext到对象.
谢谢,菲利克斯
wpf ×7
c# ×2
xaml ×2
binding ×1
code-behind ×1
combobox ×1
data-binding ×1
readonly ×1
silverlight ×1
triggers ×1
uwp-xaml ×1
windows-10 ×1