Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF ItemsControl项目模板中的访问控制_Wpf_Itemscontrol - Fatal编程技术网

WPF ItemsControl项目模板中的访问控制

WPF ItemsControl项目模板中的访问控制,wpf,itemscontrol,Wpf,Itemscontrol,我有一个ItemsControl,其中有一个简单的item模板,如下所示: <ItemsControl x:Uid="itemsControlMarketingText" x:Name="itemsControlMarketingText"> <ItemsControl.ItemTemplate> <DataTemplate x:Name="dataTemplateMarketingText"> <Bull

我有一个ItemsControl,其中有一个简单的item模板,如下所示:

<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的样式,以便它适用于填充列表时创建的所有文本块。所以基本上就像我在textblock上有一个Style属性一样

<Style="{DynamicResource BaseTextBlockMarketingText}"

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

如果在首次使用样式后需要执行此操作,则此操作将不起作用。在这种情况下,下面的替代方法将起作用

var btnStyle = FindResource("BaseTextBlockMarketingText") as Style;

//  Contrived example
btnStyle.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.YellowGreen));
或者更换它。即使已经使用了样式,这也会起作用,因为您不会更改已使用的样式对象

var btnStyle = new Style();

//  Contrived example
btnStyle.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.YellowGreen));

this.Resources["BaseTextBlockMarketingText"] = btnStyle;
XAML

不管你怎么初始化它。出于测试目的,我刚刚在视图构造函数中创建了一个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}}"
    />


它可以是viewmodel属性,但
样式实际上应该是视图的一部分

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

如果在首次使用样式后需要执行此操作,则此操作将不起作用。在这种情况下,下面的替代方法将起作用

var btnStyle = FindResource("BaseTextBlockMarketingText") as Style;

//  Contrived example
btnStyle.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.YellowGreen));
或者更换它。即使已经使用了样式,这也会起作用,因为您不会更改已使用的样式对象

var btnStyle = new Style();

//  Contrived example
btnStyle.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.YellowGreen));

this.Resources["BaseTextBlockMarketingText"] = btnStyle;
XAML

不管你怎么初始化它。出于测试目的,我刚刚在视图构造函数中创建了一个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}}"
    />

它可以是viewmodel属性,但
样式实际上应该是视图的一部分