小编haq*_*win的帖子

将一个fill属性绑定到样式中ContentControl的Foreground属性的路径中

我有银色问题我用了两天时间来打架:带有样式的模板控制按钮.在具体按钮中,我有一个以路径为内容的画布.问题是我希望路径填充颜色从模板中的ContentControl绑定到Foreground.

但是,我还没弄清楚如何构造绑定以获取Forground.如果我愿意,例如使用TextBlock,它将自动从Style中获取Forground颜色.正如预期的那样,文本具有前景色.绑定的原因是动画控制着forground,我希望它传播到路径的填充颜色.有任何想法吗?

该模板包含以下内容:

<Style x:Key="PathButtonStyle" TargetType="Button">

... Animations and state stuff

  <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" >
    <ContentControl x:Name="ContentContainer" 
      ContentTemplate="{TemplateBinding ContentTemplate}" 
      Content="{TemplateBinding Content}" 
      Foreground="{TemplateBinding Foreground}" />
  </Border>
</Style>
Run Code Online (Sandbox Code Playgroud)

在我的布局中,我有以下内容:

<Button x:Name="Play" Style="{StaticResource PathButtonStyle}" >
  <Canvas x:Name="PlayIcon">
    <Path Data="F1M191.4839,96.1763L177.9149,106.5173L177.9149,85.9293z" 
          Fill="{PATH TO CONTENTCONTROLS FOREGROUND}" />
  </Canvas>
</Button>
Run Code Online (Sandbox Code Playgroud)

我已经清理了代码并删除了一些内容以使其更具可读性,但我希望你能够了解它背后的想法.

.net silverlight silverlight-3.0 windows-phone-7

13
推荐指数
1
解决办法
2292
查看次数

使用DependencyProperty将ImageBrush绑定到模板

我正在尝试创建一个特殊按钮,根据系统中的Foreground颜色为图像着色.解决方案似乎是使用图像作为不透明蒙版来获取颜色,当我像这样直接设置图像时它可以工作:

<Grid>
  <Rectangle x:Name="ImageForeground" Height="48" Width="48" 
    Fill="{StaticResource PhoneForegroundBrush}" >
    <Rectangle.OpacityMask>
      <ImageBrush Stretch="Fill" ImageSource="/icons/play.png"/>
    </Rectangle.OpacityMask>
  </Rectangle>
</Grid>
Run Code Online (Sandbox Code Playgroud)

但是,一旦我尝试使用DependencyProperty为图像精简模板这个:

public static readonly DependencyProperty ImageProperty  =
  DependencyProperty.Register("Image", typeof(ImageSource), 
                              typeof(RButton), null);  
Run Code Online (Sandbox Code Playgroud)

然后在XAML中这样:

<Grid>
  <Rectangle x:Name="ImageForeground" Height="48" Width="48" 
    Fill="{TemplateBinding Foreground}" >
    <Rectangle.OpacityMask>
      <ImageBrush Stretch="Fill" ImageSource="{TemplateBinding Image}"/>
    </Rectangle.OpacityMask>
  </Rectangle>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说:

object of type 'System.Windows.CustomDependencyProperty' 
  cannot be converted to type 'System.Windows.DependencyProperty'
Run Code Online (Sandbox Code Playgroud)

ImageProperty没问题,因为我测试将它绑定到图像而不是像这样

<Image Source="{TemplateBinding Image}" Width="48" Height="48" />
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我的预感是如何定​​义我的DependecyProperty,但我不知道如何前进.

silverlight xaml windows-phone-7

2
推荐指数
1
解决办法
2546
查看次数