文本框在单击或焦点时不显示闪烁的光标

时间:2016-10-14 10:15:50

标签: c# .net wpf xaml

在下面代码的文本框中,即使在我点击文本框或有焦点后,闪烁的光标也不会显示。我发布了这么大的代码,因为我认为它可能是'是以某种方式干扰texbox的父元素属性,但我似乎无法找到解决方案。有人可以帮忙。

<Canvas Name="encounterTab" Style="{StaticResource canvasRecording}" Visibility="Hidden" Width="{DynamicResource {x:Static SystemParameters.FullPrimaryScreenWidthKey}}" FocusManager.IsFocusScope="True">
                <Grid Height="{DynamicResource {x:Static SystemParameters.FullPrimaryScreenHeightKey}}" Width="{DynamicResource {x:Static SystemParameters.FullPrimaryScreenWidthKey}}" Margin="0,0,0,0" FocusManager.IsFocusScope="True">
                    <DockPanel Style="{StaticResource screenTitleDock}" Grid.Row="0" VerticalAlignment="Top" >
                        <TextBlock Name="textBlock1"  Style="{StaticResource screenTitle}">ENCOUNTER DETAILS</TextBlock>
                    </DockPanel>
                    <Grid Style="{StaticResource gridRecording}" SizeChanged="MainGrid_SizeChanged" Name="gridEncDetails" FocusManager.IsFocusScope="True">
                        <Grid.LayoutTransform>
                            <ScaleTransform 
                        CenterX="0"
                        CenterY="0"
                        ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}"
                        ScaleY="{Binding ElementName=myMainWindow, Path=ScaleValue}" />
                        </Grid.LayoutTransform>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="188*"></ColumnDefinition>
                            <ColumnDefinition Width="149*"></ColumnDefinition>
                            <ColumnDefinition Width="63*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition ></RowDefinition>
                            <RowDefinition ></RowDefinition>
                            <RowDefinition ></RowDefinition>
                            <RowDefinition ></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>

                        <Label x:Name="lblApptTime" Content="Time:" Grid.Column="0" Grid.Row="0"    />
                        <TextBox x:Name="txtTime" GotKeyboardFocus="txtApptTimeKeyBoadFocus" GotMouseCapture="txtApptTime_MouseClick" Grid.Column="1" Grid.Row="0" Width="149" LostFocus="txtApptTime_LostFocus" HorizontalAlignment="Left" MouseDoubleClick="txtApptTime_MouseDoubleClick" Margin="0,11" Height="38" GotTouchCapture="txtApptTime_GotTouchCapture" />
                        <ComboBox x:Name="ddlAmPm" VerticalContentAlignment="Center" Grid.Row="0" Grid.Column="2" Width="55" IsSynchronizedWithCurrentItem="True" Margin="0,10" HorizontalAlignment="Right" Height="38">
                            <ComboBoxItem>AM</ComboBoxItem>
                            <ComboBoxItem>PM</ComboBoxItem>
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Grid >
                                        <TextBlock Height="Auto"
                       HorizontalAlignment="Stretch"
                       VerticalAlignment="Center" />
                                    </Grid>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <Label x:Name="lblNo" Content="No:" Grid.Column="0" Grid.Row="1" Margin="0,11" Height="38"  />
                        <TextBox x:Name="txtEncounterNumber" Grid.Column="1" Grid.Row="1"  KeyDown="txtEncounterNumber_KeyUp" TextChanged="txtEncounterNumber_TextChanged" HorizontalAlignment="Left" Width="212" Margin="0,10" Grid.ColumnSpan="2" Height="Auto" />
                        <Button x:Name="btnNext1" Grid.Row="2" Grid.ColumnSpan="3" Style="{StaticResource btnRec}" Click="btnNext1_Click" TouchUp="btnTouchNext1_Click" Margin="50,20,50,10" >
                            <Image Source="Assets/btnNext.png" Stretch="Fill" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Button>
                    </Grid>
                </Grid>
</Canvas >

注意: - 当我开始输入插入符号时,但当我清除文本框值时消失了。

1 个答案:

答案 0 :(得分:1)

看起来你的问题可能来自ScaleTransform。如果TextBox缩放到小于原始大小,则它的光标会消失。发生这种情况是因为TextBox插入符号的宽度为1,缩小时则小于1.因此它根本不可视化。

作为一种解决方法,将最小可能的大小设置为默认值,以便仅扩展UI。

另一种解决方法是创建自定义插入符,如此处显示WPF TextBox Inside ViewBox loses Cursor on resize