如何访问以 XAML 资源形式动态分配给控件的元素?

发布于 2024-10-21 17:51:35 字数 2297 浏览 2 评论 0原文

我的窗口中有以下资源,它声明某种 TabItem 的外观。

<Window.Resources>
    <StackPanel x:Key="TabSearchContents" x:Shared="False"
    Orientation="Vertical">
    <Border
        BorderThickness="3"
        BorderBrush="Purple">
        <TextBlock
            Text="SEARCH BOOKS"
            FontFamily="Verdana"
            FontSize="25"
            Foreground="Blue"
            HorizontalAlignment="Center" />
    </Border>
    <StackPanel
        Height="30"
        Orientation="Horizontal"
        Margin="5">

        <TextBox
            x:Name="txtSearch"
            Width="650"
            FontFamily="Comic Sans MS"
            Foreground="Chocolate" />
        <Button
            x:Name="btnSearch"
            Width="100"
            Content="Go!"
            Click="BtnSearch_Click" />
    </StackPanel>
    <Grid x:Name="gridResults">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="450"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.Column="0" VerticalScrollBarVisibility="Auto">
        <ItemsControl x:Name="itmsSearch" ItemsSource="{Binding}" Padding="4"
                ItemTemplate="{StaticResource SearchResultItemDT}">         
        </ItemsControl>
        </ScrollViewer>
        <StackPanel x:Name="stkpnlDetails">

        </StackPanel>
    </Grid>
</StackPanel>
</Window.Resources>

然后,在我的代码隐藏中,我动态创建一个选项卡并分配给窗口中已存在的 TabControl。

    void BtnNewTab_Click(object sender, RoutedEventArgs e)
    {
        TabItem tb = new TabItem();
        tb.Content = this.Resources["TabSearchContents"];               
        tb.DataContext = _bridge.SearchBooksByTitle("e");           

        tb.Header = "Wuttp yo!";
        Button btnGo = ((Button)tb.FindName("btnSearch"));
        ItemsControl i = (ItemsControl)tb.FindName("itmsSearch");
        btnGo.Resources.Add("ResultList", i);

        daTabs.Items.Add(tb);
        tb.Focus();


    }

我想访问在我的 XAML 资源中声明的 btnSearch 按钮。

实际上,此代码会抛出异常,因为 btnGo 结果为 null(以及 i),因为它无法通过 FindName() 找到预期的控件。
我读到了有关 RegisterName() 方法的信息,但它需要引用所需控件的实例...我没有。

I have the following resource in my window that declares how a certain kind of TabItem should look like.

<Window.Resources>
    <StackPanel x:Key="TabSearchContents" x:Shared="False"
    Orientation="Vertical">
    <Border
        BorderThickness="3"
        BorderBrush="Purple">
        <TextBlock
            Text="SEARCH BOOKS"
            FontFamily="Verdana"
            FontSize="25"
            Foreground="Blue"
            HorizontalAlignment="Center" />
    </Border>
    <StackPanel
        Height="30"
        Orientation="Horizontal"
        Margin="5">

        <TextBox
            x:Name="txtSearch"
            Width="650"
            FontFamily="Comic Sans MS"
            Foreground="Chocolate" />
        <Button
            x:Name="btnSearch"
            Width="100"
            Content="Go!"
            Click="BtnSearch_Click" />
    </StackPanel>
    <Grid x:Name="gridResults">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="450"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.Column="0" VerticalScrollBarVisibility="Auto">
        <ItemsControl x:Name="itmsSearch" ItemsSource="{Binding}" Padding="4"
                ItemTemplate="{StaticResource SearchResultItemDT}">         
        </ItemsControl>
        </ScrollViewer>
        <StackPanel x:Name="stkpnlDetails">

        </StackPanel>
    </Grid>
</StackPanel>
</Window.Resources>

Then, in my code-behind, I dynamically create a tab and assign to the TabControl that is already present in my window.

    void BtnNewTab_Click(object sender, RoutedEventArgs e)
    {
        TabItem tb = new TabItem();
        tb.Content = this.Resources["TabSearchContents"];               
        tb.DataContext = _bridge.SearchBooksByTitle("e");           

        tb.Header = "Wuttp yo!";
        Button btnGo = ((Button)tb.FindName("btnSearch"));
        ItemsControl i = (ItemsControl)tb.FindName("itmsSearch");
        btnGo.Resources.Add("ResultList", i);

        daTabs.Items.Add(tb);
        tb.Focus();


    }

I want to access the btnSearch Button that is declared in my XAML resource.

As it is, this code throws an exception since btnGo turns out to be null (as well as i) since it can't find the expected control via FindName().
I read about the RegisterName() method, but it requires a reference to an instance of the required control... which I don't have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2024-10-28 17:51:35

我不认为你应该像这样定义你的按钮,尝试以一种样式定义它,创建一个按钮并为按钮分配该样式,我认为你将能够通过这种方式得到你想要的东西。

myTheme.xaml

<ResourceDictionary
       <Style x:Key="btnSearch" TargetType="{x:Type Button}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Content" Value="Go!"/>
            <Setter Property="Click" Value="btn_Click"/>
       </Style>
ResourceDictionary/>

myCode.cs

Button btnGo = new Button;
btnGo.Style = "{DynamicResource btnSearch}";

希望这有帮助,
埃蒙

I dont think you should define your button like this, try defining it in a style, creating a button and assigning the button that style, i think you will be able to get what you are going for this way.

myTheme.xaml

<ResourceDictionary
       <Style x:Key="btnSearch" TargetType="{x:Type Button}">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Content" Value="Go!"/>
            <Setter Property="Click" Value="btn_Click"/>
       </Style>
ResourceDictionary/>

myCode.cs

Button btnGo = new Button;
btnGo.Style = "{DynamicResource btnSearch}";

Hope this helps,
Eamonn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文