【问题标题】:WPF ItemsControl Access Control in ItemTemplateItemTemplate 中的 WPF ItemsControl 访问控制
【发布时间】:2017-05-05 15:24:08
【问题描述】:

我有一个 ItemsControl 和一个像这样的简单项目模板:

<ItemsControl x:Uid="itemsControlMarketingText" x:Name="itemsControlMarketingText">
    <ItemsControl.ItemTemplate>
        <DataTemplate x:Name="dataTemplateMarketingText">
            <BulletDecorator x:Uid="bdMarketingTextBullet" x:Name="bdMarketingTextBullet" Width="Auto" >
                <BulletDecorator.Bullet>
                    <Ellipse Width="5" Height="5" Fill="Black" Margin="8,0,0,0"></Ellipse>
                </BulletDecorator.Bullet>
                <TextBlock x:Uid="tbMarketingTextItem" x:Name="tbMarketingTextItem" Text="{Binding}" ></TextBlock>
            </BulletDecorator>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我想在后面的代码中设置 tbMarketingTextItem 的样式,因此它适用于在填充列表时创建的所有文本块。所以基本上就像我在文本块上有一个 Style 属性一样

<Style="{DynamicResource BaseTextBlockMarketingText}"

问题在于这个特定的应用程序,它正在做一些事情来合并不同的样式表并在代码隐藏中应用样式。有没有办法从数据模板中获取控件以应用样式?我怀疑我必须以某种方式使用“FindName”方法,但我无法弄清楚如何。

【问题讨论】:

    标签: wpf itemscontrol


    【解决方案1】:

    只需使用FindResource() 获取已定义为资源的样式,然后从您获取它们的任何位置将设置器等合并到其中。

    如果您在第一次使用样式后需要这样做,这将不起作用。在这种情况下,下面的替代方法将起作用。

    var btnStyle = FindResource("BaseTextBlockMarketingText") as Style;
    
    //  Contrived example
    btnStyle.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.YellowGreen));
    

    或者替换它。即使样式已经被使用,这也将起作用,因为您没有更改使用的 Style 对象。

    var btnStyle = new Style();
    
    //  Contrived example
    btnStyle.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.YellowGreen));
    
    this.Resources["BaseTextBlockMarketingText"] = btnStyle;
    

    XAML

    <Style 
        x:Key="BaseTextBlockMarketingText" 
        TargetType="Button" 
        BasedOn="{StaticResource {x:Type Button}}"
        />
    

    ...

    <Button
        Style="{DynamicResource BaseTextBlockMarketingText}"
        />
    

    替代解决方案

    使按钮样式成为视图的属性。 Window,UserControl,没关系。下面我的 XAML 将假定它是一个窗口,但这并不重要。

    这让我们可以替换样式而不是更改现有样式实例。

    #region ButtonStyle Property
    public Style ButtonStyle
    {
        get { return (Style)GetValue(ButtonStyleProperty); }
        set { SetValue(ButtonStyleProperty, value); }
    }
    
    public static readonly DependencyProperty ButtonStyleProperty =
        DependencyProperty.Register(nameof(ButtonStyle), typeof(Style), typeof(MainWindow),
            new PropertyMetadata(null));
    #endregion ButtonStyle Property
    

    随意初始化它。出于测试目的,我只是在视图构造函数中创建了一个 quickie:

    public MainWindow()
    {
        InitializeComponent();
    
        var style = new Style(typeof(Button), FindResource(typeof(Button)) as Style);
    
        style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.LightSkyBlue));
    
        ButtonStyle = style;
    }
    

    并像这样使用它。这里的AncestorType 参数是唯一重要的地方,ButtonStyle 是一个窗口的属性,特别是:

    <Button
        Style="{Binding ButtonStyle, RelativeSource={RelativeSource AncestorType=Window}}"
        />
    

    可能是一个视图模型属性,但 Style 确实应该是视图的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多