WPF Datagrid - 1st Row show checkbox, 2nd Row Hide Checkbox,...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mathias123
    New Member
    • Nov 2012
    • 10

    WPF Datagrid - 1st Row show checkbox, 2nd Row Hide Checkbox,...

    hello! Its me again. I hope somebody can help me!

    I don't know if it is possible but i hope so...

    i have a datagrid with 2 Columns:

    1 Column is a checkbox (template)
    Code:
    <DataGridTemplateColumn Header="geprüft">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox Name="chk" Content="{Binding Path=geprüft}" IsChecked="False" IsThreeState="False"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    and the last Column is a textbox binding with status

    Code:
    <DataGridTextColumn IsReadOnly="True" Width="auto" Header="status" Binding="{Binding Status}">
    now, i only want to show the checkbox if status = 4... so in row1 = show, row2 = hide etc...

    is it possible?

    and how can i make an event on the checkbox (to save in db if checkbox is checked)

    if i make an event:
    Code:
    Private Sub chk_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles chk.Click
    End Sub
    I get an error:
    Error Handles clause requires a WithEvents variable defined in the containing type or one of its base types.


    hope somebody can help me!

    Nice Greets,
    Mathias
    Attached Files
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Hi Mathias,

    Have you ever used converters before?
    Create a class that implements the IValueConverter Interface so that you can bind your IsChecked property to the Status property. The converter will convert the 3, or 4 into a true or false value so that the IsChecked property will work.

    -Frinny

    Comment

    • Mathias123
      New Member
      • Nov 2012
      • 10

      #3
      Hello Frinny,
      Thank you for your response! i try to do that... and hope it works.

      But i think you don't understand what i need. (because my english is not so good)


      1) I only want to see the checkbox when status = 4
      If status = 3 or status = 2 or status = 1 then Hide the checkbox in the Row. (only visible on status = 4)

      2) If i checked the checkbox in the row with status 4 i want to make an event in the Database. The Status should get the value 5 (Status 5 = Finished)


      so is in this case the IValueConverter Interface the best solution?

      nice Greets,
      Mathias
      Attached Files

      Comment

      • Mathias123
        New Member
        • Nov 2012
        • 10

        #4
        ok now i have a solution for the checkbox to save something in database... i only add Click="CheckBox _Click"

        Code to go in a sub :)
        Code:
         
        <DataGridTemplateColumn Header="geprüft"> 
            <DataGridTemplateColumn.CellTemplate> 
                <DataTemplate> 
                    <CheckBox Name="chk" Content="{Binding Path=geprüft}" IsChecked="False" IsThreeState="False" Click="CheckBox_Click"/> 
                </DataTemplate> 
            </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn>


        But i have no solution for the first problem (checkbox visible or hidden for each rows)


        Good Night, Good Fight!


        Nice Greets,
        Mathias
        Last edited by Frinavale; Nov 20 '12, 04:22 PM. Reason: Moved text out of the code block.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          You can get around this without using converters than...just use triggers like before but apply it to the CheckBox:

          Code:
          <DataGridTemplateColumn>
              <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                      <CheckBox Name="chk" Content="{Binding Path=geprüft}" IsThreeState="False" Visibility="Collapsed">
                          <CheckBox.Style>
                              <Style TargetType="CheckBox">
                                  <Style.Triggers>
                                      <DataTrigger Binding="{Binding Path=Status}" Value="4">
                                          <Setter Property="Visibility" Value="Visible" />
                                      </DataTrigger>
                                  </Style.Triggers>
                              </Style>
                          </CheckBox.Style>
                      </CheckBox>
                  </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>

          It seems that you have solved your second part of the question.

          -Frinny

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            If you weren't using triggers, and you wanted to try a converter you would do the following.

            You would create a class that implements the IValueConverter interface. The purpose of this class would be to return the visibility (collapsed or visible) depending on the Status value. The convert back method would set the status to 5...

            I'm just not 100% sure if it would work because I can't really try it....

            But your class would look something like this:
            (VB.NET)
            Code:
            Public Class StatusVisibilityConverter
                Implements IValueConverter
            
                Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
                    Dim is4Status As Boolean = CType(value, Integer)
            
                    If is4Status = 4 Then
                        Return Visibility.Visible
                    End If
            
                    Return Visibility.Collapsed
                End Function
            
                Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
                    Throw New NotImplementedException
                    'In here you would have to try converting back somehow...I'm just not sure how
                End Function
            End Class
            (C#)
            Code:
            public class StatusVisibilityConverter : IValueConverter
            {
            
            	public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
            	{
            		bool is4Status = Convert.ToInt32(value);
            
            		if (is4Status == 4) {
            			return Visibility.Visible;
            		}
            
            		return Visibility.Collapsed;
            	}
            
            	public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
            	{
            		throw new NotImplementedException();
            		//In here you would have to try converting back somehow...I'm just not sure how
            	}
            }
            Add your project's namespace to your window/user control/page:
            Code:
            <Window x:Class="Window1"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:myProject="clr-namespace:MyProjectNamespace"
                    Title="Window1" Height="760" Width="800"
                    Background="White">
            Add your visibility converter as a resource to the Window/User Control/Page...or even as a resource to your grid...so that you can use it:
            Code:
            <Window.Resources>
                <myProject:StatusVisibilityConverter x:Key="StatusVisibilityConverter" />
            </Window.Resources>
            Then change the XAML code for your CheckBox so that it's Visibility property is bound to your Status property and uses your new converter:
            Code:
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Name="chk" Content="{Binding Path=geprüft}" IsChecked="{Binding Path=Status}" IsThreeState="False"
                                  Visibility="{Binding Path=Status, Converter={StaticResource StatusVisibilityConverter}}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            -Frinny

            Comment

            • Mathias123
              New Member
              • Nov 2012
              • 10

              #7
              Hi!

              I try the first post (because I'm new in WPF) but it doesn't work... the property
              Code:
              <Setter Property="Visibility" Value="Visible" />
              doesn't work?!

              i try another Property
              Code:
              <Setter Property="IsEnabled" Value="false"/>
              and it works... All other Properties work, but i don't know why the Property "Visibility " doesn't work...

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                It would help if you posted the whole style because it worked when I tried it before posting...

                -Frinny

                Comment

                • Mathias123
                  New Member
                  • Nov 2012
                  • 10

                  #9
                  now i tried your second post and it work!

                  thank you for your help!!!!!

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    I'm glad it works.

                    Just make sure you implement the "convert back" method so that it works properly.

                    -Frinny

                    Comment

                    Working...