값을 변경의 사용자 정의 종속성 프로그래밍 방식으로

0

질문

내가 원하는 값을 변경하려면 사용자 정의 종속성 프로그래밍 방식으로 보여줍니다.

이것은 내 XAML:

<Window.Resources>
      <ResourceDictionary>
         <Style
            x:Key="TreeViewItemStyle"
            TargetType="TreeViewItem">    
            <Style.Triggers>
               <Trigger
                  Property="local:ColorHelper.IsColor"
                  Value="True" >
               <Setter
                  Property="Foreground"
                  Value="{Binding Color}" />
               </Trigger>
               <Trigger
                  Property="local:ColorHelper.IsColor"
                  Value="False" >
               <Setter
                  Property="Foreground"
                  Value="Black" />
               </Trigger>
            </Style.Triggers>
           </Style>
      </ResourceDictionary>
   </Window.Resources>

나는 이 스타일에 적용되는 트

<TreeView
           HorizontalAlignment="Stretch"
           Margin="15,65,15,0"
           x:Name="treeView1"
           VerticalAlignment="Stretch"
           ItemContainerStyle="{StaticResource TreeViewItemStyle}"
           ItemTemplate="{StaticResource CheckBoxItemTemplate}"
           Grid.ColumnSpan="1"
           Grid.RowSpan="2"
           Grid.Column="1" />

고 변화하는 색상의 가치 IsColor 속성을 통해 확인란을 선택합니다:

<CheckBox
                     Name="CHK_Gray"
                     VerticalAlignment="Center"
                     Foreground="DarkGray"
                     Grid.Row="6"
                     Grid.Column="0"
                     Grid.RowSpan="1"
                     Grid.ColumnSpan="2"
                     Unchecked="grayCheckBox_Unchecked"
                     Checked="grayCheckBox_Checked">
                     Show Created Assembly (in Grey)
                  </CheckBox>

종속성 제공을 만든 다음과 같다:

 public class ColorHelper : DependencyObject
   {
      public static readonly DependencyProperty IsColorProperty = DependencyProperty.Register(
          "IsColor", typeof(bool), typeof(ColorHelper), new PropertyMetadata(false));


      public static void SetIsColor(DependencyObject target, Boolean value)
      {
         target.SetValue(IsColorProperty, value);
      }

      public static bool GetIsColor(DependencyObject target)
      {
         return (bool)target.GetValue(IsColorProperty);
      }
   }

어떻게 할 수 있습니에탁 및 이벤트 변경의 가치 IsColor 설턴트를 제공합니다.

private void grayCheckBox_Checked(object sender, RoutedEventArgs e)
  {
     ???
  }
  private void grayCheckBox_Unchecked(object sender, RoutedEventArgs e)
  {
     ???
  }

매우 감사드립니다.

c# data-binding wpf
2021-11-23 18:29:39
1

최고의 응답

0

을 얻을 필요가있을 참조하는 요소를 설정하려면 연결 속으로 시작 됩니다.

시도할 수 있습니다 이것 을 얻는 방법 TreeViewItem 의 요소 TreeView 다음 설정의 자산이 모든 그들의 경우는 당신이 원하는:

foreach(TreeViewItem tvi i tv.FindTreeViewItems())
    ColorHelper.SetIsColor(tvi, true);

참고 편집할 수 없습니다 템플릿을 프로그래밍 방식으로 보여줍니다. 심지어할 수 있다면,그것은 없을 것이 정말로 도움으로 그것이 이미 적용된 실제적인 요소입니다.

2021-11-24 14:43:11

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................