簡體   English   中英

從UserControl.Resources中的ControlTemplate訪問文本框

[英]Access textbox from ControlTemplate in UserControl.Resources

我有一個定義如下的控制模板:

<UserControl.Resources>
 <ControlTemplate TargetType="{x:Type DataGrid}"  x:Key="inputItemsControlTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="20"/>
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                </Grid.RowDefinitions>
                <Label Grid.Row="1" Content="{DynamicResource UCodeStr}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="27" />
                <TextBox Name="txtUCode" Grid.Row="2" Height="23" HorizontalAlignment="Left" VerticalAlignment="Bottom"  Width="100" Text="{Binding UCode, UpdateSourceTrigger=PropertyChanged}" />
            </Grid>
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                </Grid.RowDefinitions>
                <Label Grid.Row="1" Content="{DynamicResource GoodStr}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="27"/>
                <ComboBox Grid.Row="2" Height="23" Width="189" HorizontalAlignment="Left"  Name="cbGoods" ItemsSource="{Binding Path=Goods}"  SelectedItem="{Binding Path= Good, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" IsEditable="True" IsTextSearchEnabled="True" TextSearch.TextPath="Name" />
                <Label Grid.Row="3" Content="{DynamicResource InputPriceStr}" HorizontalAlignment="Left" Name="lblInputPrice" VerticalAlignment="Bottom" Height="27"/>
                <TextBox Name="txtInputPrice" Grid.Row="4" TextAlignment="Right" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path= InputPrice, UpdateSourceTrigger=PropertyChanged,  ValidatesOnDataErrors=True, NotifyOnValidationError=True, StringFormat='N2'}" />                   
            </Grid>
            <Grid Grid.Column="3">
                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                </Grid.RowDefinitions>
                <Label Grid.Row="1" Content="{DynamicResource AmmountStr}" HorizontalAlignment="Left" Name="lblAmmount" VerticalAlignment="Bottom" Height="27"/>
                <TextBox Name="txtAmmount" TextAlignment="Right"  Grid.Row="2" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path=Amount,  ValidatesOnDataErrors=True, NotifyOnValidationError=True,  UpdateSourceTrigger=PropertyChanged, StringFormat='N2'}" />                   
                <Label Grid.Row="3" Content="{DynamicResource SuggestedPriceStr}" HorizontalAlignment="Left" Name="lblSuggestedPrice" VerticalAlignment="Bottom" Height="27"/>
                <TextBox Name="txtSuggestedPrice" Grid.Row="4" TextAlignment="Right" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path= SuggestedPrice,  ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, StringFormat='N2'}" />
                <CheckBox Grid.Row="5" Name="cbHasVatDeduction"  IsChecked="{Binding Path=HasVatDeduction}" />
            </Grid>
        </Grid>
    </ControlTemplate>
</UserControl.Resources>

我在代碼隱藏中將這個模板實現為datagrid。 我想訪問文本框“ txtUCode”並將其設置為焦點。 我這樣嘗試過:

InputDocItemsDataGrid.Template = (ControlTemplate)this.FindResource("inputItemsControlTemplate");
        TextBox txtUCode = (TextBox)InputDocItemsDataGrid.Template.FindName("txtUCode", InputDocItemsDataGrid);
        txtUCode.Focus();

但是我的txtUCode始終為null。 這個怎么做?

原則上,您的代碼是好的。 但是在分配這樣的模板時:

InputDocItemsDataGrid.Template = (ControlTemplate)this.FindResource("inputItemsControlTemplate");

控件加載后,即元素布局,渲染並准備好進行交互后,即可訪問其元素。

因此,請嘗試以下示例:

XAML

<Window x:Class="SampleDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded"
        ContentRendered="Window_ContentRendered">

    <Window.Resources>
        <ControlTemplate x:Key="inputItemsControlTemplate" TargetType="{x:Type DataGrid}">
            <Grid>
             ....        
            </Grid>
       </ControlTemplate>
    </Window.Resources>

    <Grid>
        <DataGrid Name="InputDocItemsDataGrid" />
    </Grid>
</Window>

Code-behind

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    InputDocItemsDataGrid.Template = (ControlTemplate)this.FindResource("inputItemsControlTemplate");
}

private void Window_ContentRendered(object sender, EventArgs e)
{            
    TextBox txtUCode = (TextBox)InputDocItemsDataGrid.Template.FindName("txtUCode", InputDocItemsDataGrid);
    txtUCode.Focus();
}

首先,它調用Window_Loaded事件,然后調用Window_ContentRendered

或像這樣:

XAML

<Grid>
    <DataGrid Name="InputDocItemsDataGrid" Loaded="InputDocItemsDataGrid_Loaded" />

    <Button Content="Test" HorizontalAlignment="Left" VerticalAlignment="Bottom" Click="Button_Click" />
</Grid>

Code-behind

private void InputDocItemsDataGrid_Loaded(object sender, RoutedEventArgs e)
{
    InputDocItemsDataGrid.Template = (ControlTemplate)this.FindResource("inputItemsControlTemplate");
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    TextBox txtUCode = (TextBox)InputDocItemsDataGrid.Template.FindName("txtUCode", InputDocItemsDataGrid);
    txtUCode.Focus();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM