1

这对我不起作用

<UserControl x:Class="BenchmarkPlus.PMT.UI.Views.Circle"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" x:Name="root"
             DataContext="{Binding Source={x:Static Brushes.Blue}}">
    <UserControl.Resources>
        <Style TargetType="Ellipse">
            <Setter Property="Fill" Value="{Binding DataContext}" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <Ellipse Stroke="Black"></Ellipse>
    </Grid>
</UserControl>

在此处输入图像描述

4

2 回答 2

4

使其简单DataContext="{x:Static Brushes.Blue}"并更改Value="{Binding DataContext}"Value="{Binding}".

当您在绑定表达式中指定路径时,它始终与您的 DataContext 相关,因此当您使用 时{Binding DataContext},您实际上是绑定到DataContext.DataContext,这不是您想要的。

于 2012-04-19T23:09:02.687 回答
1

我想你想要

<UserControl x:Class="BenchmarkPlus.PMT.UI.Views.Circle" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" x:Name="root" 
             DataContext="{x:Static Brushes.Blue}"> 
    <UserControl.Resources> 
        <Style TargetType="Ellipse"> 
            <Setter Property="Fill" Value="{Binding}" /> 
        </Style> 
    </UserControl.Resources> 
    <Grid> 
        <Ellipse Stroke="Black"></Ellipse> 
    </Grid> 
</UserControl> 
于 2012-04-19T23:19:31.743 回答