2011-04-10 68 views
2

我正在C#中模拟最小生成树算法。对于图中的每个顶点,我使用复选框作为可视化表示。每次将顶点添加到最小生成树时,我都想更改复选框的颜色。 复选框使用表达式混合4设计,它已经具有基本属性(基础,鼠标悬停,选定等)。基本颜色是黑色的,我希望当顶点被添加到树中时它是绿色的。如何动态地改变C中复选框的颜色#

这是我如何使用复选框的例子:

private void DeselectAll() 
{ 
    foreach (var n in graf.Noduri) 
    { 
     CheckBox c = (CheckBox)n.graficNod.Content; 
     c.IsChecked = false; 
    } 
} 

,其中n是具有graficNod,其在表达共混创建,使用XAML类型的Nod的属性类型CNOd所的。

如何更改graficNod的基本颜色?

Nod.xaml看起来是这样的:

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
mc:Ignorable="d" 
x:Class="SimulareGrafuri.Nod" 
x:Name="UserControl" 
Width="10" 
Height="10"  
d:DesignWidth="640" d:DesignHeight="480"> 
<UserControl.Resources> 
    <ControlTemplate x:Key="CheckBoxControlTemplate1" TargetType="{x:Type CheckBox}"> 
     <Grid> 
      <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup x:Name="CommonStates"> 
        <VisualState x:Name="Normal"/> 
        <VisualState x:Name="MouseOver"> 
         <Storyboard> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="ellipse"> 
           <EasingColorKeyFrame KeyTime="0" Value="#FFD0CACA"/> 
          </ColorAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="ellipse"> 
           <EasingColorKeyFrame KeyTime="0" Value="Black"/> 
          </ColorAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeThickness)" Storyboard.TargetName="ellipse"> 
           <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
          </DoubleAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="Pressed"> 
         <Storyboard> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="ellipse"> 
           <EasingColorKeyFrame KeyTime="0" Value="Black"/> 
          </ColorAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="ellipse"> 
           <EasingColorKeyFrame KeyTime="0" Value="#FF5A5454"/> 
          </ColorAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="Disabled"/> 
       </VisualStateGroup> 
       <VisualStateGroup x:Name="CheckStates"> 
        <VisualState x:Name="Checked"> 
         <Storyboard> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="ellipse"> 
           <EasingColorKeyFrame KeyTime="0" Value="Black"/> 
          </ColorAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="rectangle"> 
           <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
          </DoubleAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle"> 
           <EasingColorKeyFrame KeyTime="0" Value="#FF540000"/> 
          </ColorAnimationUsingKeyFrames> 
          <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeThickness)" Storyboard.TargetName="rectangle"> 
           <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
          </DoubleAnimationUsingKeyFrames> 
          <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="rectangle"> 
           <EasingThicknessKeyFrame KeyTime="0" Value="-2"/> 
          </ThicknessAnimationUsingKeyFrames> 
          <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="ellipse"> 
           <EasingColorKeyFrame KeyTime="0" Value="#FFA70808"/> 
          </ColorAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="Unchecked"> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="rectangle"> 
           <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="Indeterminate"/> 
       </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 
      <Ellipse x:Name="ellipse" Fill="Black" Margin="0" Stroke="Black" Width="Auto"/> 
      <Rectangle x:Name="rectangle" Fill="{x:Null}" Margin="6,6,82,72" Opacity="0" Stroke="Black"/> 
     </Grid> 
    </ControlTemplate> 
</UserControl.Resources> 



<CheckBox Content="CheckBox" Template="{DynamicResource CheckBoxControlTemplate1}"/> 

+1

设置yourCheckbox.Foreground是否适合您? http://msdn.microsoft.com/en-us/library/system.windows.controls.control.foreground.aspx – 2011-04-13 06:44:49

回答

2

简单: 我看你已经改变了复选框不少类似于一个顶点,但现在它将始终,是一个黑色的椭圆因为你设置的“填充”明确:

<Ellipse x:Name="ellipse" Fill="Black" Margin="0" Stroke="Black" Width="Auto"/> 

改为使用TemplateBinding这将改变,只要你设定的复选框的背景属性填充:

<Ellipse x:Name="ellipse" Fill="{TemplateBinding Background}" Margin="0" Stroke="Black" Width="Auto"/> 

,改变你的复选框声明:

<CheckBox x:Name="chkVertex" Content="CheckBox" Template="{DynamicResource CheckBoxControlTemplate1}" Background="Red"/> 

现在你可以改变颜色的代码隐藏,只要你喜欢,使用:

chkVertex.Background = //whatever color you like 

,或者你可以绑定背景将xaml中的复选框的属性复制到顶点的背景属性中,以获得更清晰的方法。