【问题标题】:Accessing Style setters in code在代码中访问样式设置器
【发布时间】:2023-04-04 12:14:01
【问题描述】:

我正在尝试在 ListBox 上实现捏合/拉伸缩放:

<ListBox
   Grid.Row="1"
   ManipulationCompleted="ListBox_ManipulationCompleted"
   ItemsSource="{Binding Paras}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <TextBlock
         DataContext="{Binding}"
         TextWrapping="Wrap"
         Text="{Binding Text}"
         FontSize="{Binding FontSize}">
       </TextBlock>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

我真正想做的是将 RenderTransform 绑定到我的 ViewModel 的 Zoom 属性,但这是不允许的。我可以绑定到 FontSize 并正确更改 FontSize,但我必须在我的代码隐藏中对其进行硬编码。我真正想做的是尊重设计师设置的 FontSize 设置,并对所有 FontSize 应用统一缩放。所以在我后面的代码中:

Style style = App.Current.Resources[_XElement.Name.LocalName] as Style;
if ( style != null )
{
  foreach ( var s in style.Setters )
  {
    Setter setter = s as Setter;
    if ( setter != null )
    {
        Debug.WriteLine(setter.Property + "=" + setter.Value);
    }
  }
}

我无法访问 setter.Property.Name(编译时错误),尽管我可以看到它是 FontSize 在调试器中。并且值始终是“无法评估表达式” - 输出为空白。

任何线索,无论是关于这里发生了什么,还是更好的方法?

安德鲁

【问题讨论】:

    标签: silverlight data-binding windows-phone-7


    【解决方案1】:

    您无法访问 Name,因为 DependencyProperty 的 Silverlight 实现不会公开名称。为了获取设置FontSize 属性的设置器,您需要使用一点反射从Style.TargetType 中挑选出一个名为“FontSizeProperty”的静态字段并提取其值。然后将每个 setter Property 与该值进行比较,如果您有匹配项,则您已找到负责 FontSize 的 setter。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2017-03-28
      • 2011-09-30
      • 2016-10-16
      • 1970-01-01
      相关资源
      最近更新 更多