WPF - List View Row Index and Validation

Posted by abhishek on Stack Overflow See other posts from Stack Overflow or by abhishek
Published on 2009-02-10T15:47:17Z Indexed on 2010/04/23 12:23 UTC
Read the original article Hit count: 763

Filed under:
|
|

Hi,

I have a ListView with TextBoxes in second column. I want to validate that my text box does not contain a number if the third column(data_type) is "Text". I am unable to do the validation.

I tried a few approaches. In one approach I try to handle the MouseDown event and am trying to get the Row number so that I can get the data_type value of that row. I want to us this value in the Validate method.

I have been struggling for a week now. Would appreciate if anybody could help.

<ControlTemplate x:Key="validationTemplate">
  <DockPanel>
    	<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
    	<AdornedElementPlaceholder/>
  </DockPanel>
</ControlTemplate>

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </Style.Triggers>
</Style>

    <DataTemplate x:Key="textTemplate">
        <TextBox  HorizontalAlignment= "Stretch" 
	IsEnabled="{Binding XPath=./@isenabled}"		
	Validation.ErrorTemplate="{StaticResource validationTemplate}"		
    Style="{StaticResource textBoxInError}">
            <TextBox.Text>
                <Binding XPath="./@value" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>   		
                        <local:TextBoxMinMaxValidation>
		    <local:TextBoxMinMaxValidation.DataType>
                          <local:DataTypeCheck 
                                Datatype="{Binding Source={StaticResource dataProvider}, XPath='/[@id=CustomerServiceQueueName]'}"/>
                        </local:TextBoxMinMaxValidation.DataType>     		   
                         <local:TextBoxMinMaxValidation.ValidRange>
			<local:Int32RangeChecker 
                                Minimum="{Binding Source={StaticResource dataProvider}, XPath=./@min}" 
                                Maximum="{Binding Source={StaticResource dataProvider}, XPath=./@max}"/>    			                            
		     </local:TextBoxMinMaxValidation.ValidRange>
                        </local:TextBoxMinMaxValidation>
                    </Binding.ValidationRules> 
                </Binding >
            </TextBox.Text> 
        </TextBox>
    </DataTemplate>
    <DataTemplate x:Key="dropDownTemplate">
        <ComboBox  Name="cmbBox" HorizontalAlignment="Stretch"
	       SelectedIndex="{Binding XPath=./@value}"
           ItemsSource="{Binding XPath=.//OPTION/@value}"
           IsEnabled="{Binding XPath=./@isenabled}"
         />
    </DataTemplate>
<DataTemplate x:Key="booldropDownTemplate">
        <ComboBox  Name="cmbBox" HorizontalAlignment="Stretch"
	       SelectedIndex="{Binding XPath=./@value,  Converter={StaticResource boolconvert}}">
	        <ComboBoxItem>True</ComboBoxItem>
	        <ComboBoxItem>False</ComboBoxItem>                                         
        </ComboBox>
    </DataTemplate>
    <local:ControlTemplateSelector 
        x:Key="myControlTemplateSelector"/>

    <Style x:Key="StretchedContainerStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="Template" Value="{DynamicResource ListBoxItemControlTemplate1}"/>
    </Style>
    <ControlTemplate x:Key="ListBoxItemControlTemplate1" TargetType="{x:Type ListBoxItem}">
        <Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" Padding="{TemplateBinding Padding}" BorderThickness="0,0.5,0,0.5">
            <GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Border>            
    </ControlTemplate>


    <Style x:Key="CustomHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="Background" Value="LightGray" />            
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="Padding" Value="2,0,2,0"/>	             
    </Style>

</UserControl.Resources>
<Grid x:Name="GridViewControl" Height="Auto">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="34"/>
    </Grid.RowDefinitions>

    <ListView x:Name="ListViewControl" Grid.Row="0"   ItemContainerStyle="{DynamicResource StretchedContainerStyle}" 
	ItemTemplateSelector="{DynamicResource myControlTemplateSelector}" 
	IsSynchronizedWithCurrentItem="True"
	ItemsSource="{Binding Source={StaticResource dataProvider}, 
	XPath=//CONFIGURATION}">           

        <ListView.View >
            <GridView >                    
                <GridViewColumn  Header="ID" HeaderContainerStyle="{StaticResource CustomHeaderStyle}" DisplayMemberBinding="{Binding XPath=./@id}"/>
                <GridViewColumn  Header="VALUE" HeaderContainerStyle="{StaticResource CustomHeaderStyle}" CellTemplateSelector="{DynamicResource myControlTemplateSelector}" />
                <GridViewColumn  Header="DATATYPE" HeaderContainerStyle="{StaticResource CustomHeaderStyle}" DisplayMemberBinding="{Binding XPath=./@data_type}"/>
                <GridViewColumn  Header="DESCRIPTION" HeaderContainerStyle="{StaticResource CustomHeaderStyle}"  
		DisplayMemberBinding="{Binding XPath=./@description}" 
		Width="{Binding ElementName=ListViewControl, Path=ActualWidth}"/>


            </GridView>
        </ListView.View>

    </ListView>
    <StackPanel Grid.Row="1">
        <Button Grid.Row="1" HorizontalAlignment="Stretch"  Height="34" HorizontalContentAlignment="Stretch" >
            <StackPanel  HorizontalAlignment="Stretch"  VerticalAlignment="Center"  Orientation="Horizontal"  FlowDirection="RightToLeft" Height="30">
                <Button  Grid.Row="1" Content ="Apply" Padding="0,0,0,0 " Margin="6,2,0,2" Name="btn_Apply" HorizontalAlignment="Right"  VerticalContentAlignment="Center"   HorizontalContentAlignment="Center" Width="132"  IsTabStop="True" Click="btn_ApplyClick"  Height="24" />
            </StackPanel >
        </Button> 
    </StackPanel >

</Grid>

© Stack Overflow or respective owner

Related posts about listview

Related posts about validation