2011-04-10 5 views
2

C#에서 최소 스패닝 트리 알고리즘을 시뮬레이션하고 있습니다. 그래프의 각 정점에 대해 시각적 표현으로 확인란을 사용하고 있습니다. 버텍스가 최소 스패닝 트리에 추가 될 때마다 확인란의 색상을 변경하고 싶습니다. 체크 상자는 표현식 블렌드 4를 사용하여 설계되었으며 기본 속성 (기본, 마우스 오버, 선택 등)을 이미 가지고 있습니다. 기본 색상은 검정색이며 꼭지점이 트리에 추가되면 초록색이되고 싶습니다. C#에서 확인란의 색상을 동적으로 변경하는 방법

내가 체크 박스를 사용하고 방법의 예입니다 :

n은 XAML을 사용, 표현의 조화에서 만든 형 놋의 속성으로 graficNod이있는 타입 cNod이다
private void DeselectAll() 
{ 
    foreach (var n in graf.Noduri) 
    { 
     CheckBox c = (CheckBox)n.graficNod.Content; 
     c.IsChecked = false; 
    } 
} 

.

어떻게 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

귀하의 확인란을 설정하고 있습니까? http://msdn.microsoft.com/en-us/library/system.windows.controls.control.foreground.aspx –

답변

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"/> 

과 당신의 체크 박스를 선언 변경 :

chkVertex.Background = //whatever color you like 

하거나 배경을 결합 수 :

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

를 원할 때마다 당신은 사용 뒤에 코드에서 현재 색상을 변경할 수 있습니다 버텍스의 배경 속성에 대한 xaml의 체크 박스 속성.

관련 문제