如何设置默认选择的字段

时间:2010-12-17 20:12:30

标签: wpf xaml

你好,我不知道如何设置默认选择的字段 这是我的代码:

<DockPanel>
        <Grid DockPanel.Dock="Left" Height="426">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="63"/>
                <RowDefinition Height="79"/>
            </Grid.RowDefinitions>
            <Label Foreground="blue" Margin="0,1,0,4" Height="26">Message Number:</Label>
            <Label Grid.Row="1" Foreground="blue" Margin="0 10 0 0">Title:</Label>
            <Label Grid.Row="2" Foreground="blue" >Message Date:</Label>
            <Label Grid.Row="3" Foreground="blue" Margin="0 10 0 0">Contact Mail:</Label>
            <Label Grid.Row="4" Foreground="blue" Margin="0 10 0 0">Message Type:</Label>
            <Label Grid.Row="5" Foreground="blue" Margin="0 10 0 0">Message Details:</Label>
            <Button Grid.Row="6" Name="saveBtn" Background="White" Margin="6,20,11,36" Width="89" Click="saveBtn_Click">Save</Button>
            <TextBox Grid.Column="1" Name="IDtbx" Margin="0 10 0 0"></TextBox>
            <TextBox Grid.Row="1" Grid.Column="1" Name="titletbx" Margin="0 10 0 0"></TextBox>
            <Calendar Grid.Row="2" Grid.Column="1" Name="calender"></Calendar>
            <TextBox Grid.Row="3" Grid.Column="1" Name="mailtbx" Margin="0 10 0 0"></TextBox>
            <ComboBox Grid.Row="4" Grid.Column="1" Name="typecbx" Margin="0 10 0 0"></ComboBox>
            <TextBox Grid.Row="5" Grid.Column="1" Name="detailstbx" Margin="0,10,0,1" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>
        </Grid>
        <DockPanel>
            <Grid DockPanel.Dock="Left" Height="406">
                <Grid.RowDefinitions>
                    <RowDefinition Height="372"></RowDefinition>

                    <RowDefinition Height="7"/>
                    <RowDefinition Height="27*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Border CornerRadius="6" BorderBrush="Purple" BorderThickness="3" Margin="18,8,0,7" Grid.RowSpan="2" Width="182">
                    <Image Grid.Row="1" Name="messageImage" Height="359" Width="177"></Image>
                </Border>
                <Button Name="ChangeImagebtn" Background="white" Content="Change Image" Grid.Row="2" Margin="21,2,0,-2" Click="ChangeImagebtn_Click"></Button>
            </Grid>
        </DockPanel>

    </DockPanel>

当我运行程序时,出于某种原因选择了保存按钮,我不希望它出现。 在此先感谢您的帮助

1 个答案:

答案 0 :(得分:2)

我认为第一个按钮是被选中的按钮 - 但我可能错了。

您可以通过将另一个按钮的IsDefault属性设置为true来指定您想要成为默认按钮的按钮。

如果要将焦点设置为文本框,则可以使用:

FocusManager.FocusedElement="{Binding ElementName=myTextBox}"
Window定义中的

MSDN page