简体   繁体   English

WPF:在XAML中多次使用元素

[英]WPF: Use element multiple times in XAML

For example I have an element in some XAML view. 例如,我在某些XAML视图中有一个元素。

<TextBox x:Name="SomeName" Text="{Binding SomeText}"></TextBox>

<Grid>
    <!-- TODO: Render "SomeName" here-->
</Grid>
<Grid>
    <!-- TODO: Render "SomeName" here also-->
</Grid>

Is it possible? 可能吗?

For example by ContentControl or something... 例如通过ContentControl之类的东西

You're right about the ContentControl idea, you just need to use it with a "DataTemplate" 您对ContentControl的想法是正确的,只需要将其与“ DataTemplate”一起使用

<UserControl.Resources>

    <DataTemplate x:Key="SomeNameTemplate">
        <TextBox Text="{Binding}" />
    </DataTemplate>

<UserControl.Resources>

<StackPanel>
    <ContentControl Content="{Binding SomeText}" ContentTemplate="{StaticResource SomeNameTemplate}" />
    <ContentControl Content="{Binding SomeText}" ContentTemplate="{StaticResource SomeNameTemplate}" />
    <ContentControl Content="{Binding SomeText}" ContentTemplate="{StaticResource SomeNameTemplate}" />
</StackPanel>

EDIT: 编辑:

The ContentControl and any control with a "Content" field can automatically select it's template by type. ContentControl和带有“ Content”字段的任何控件都可以按类型自动选择其模板。

// Let's define a collection item type
class Item
{
    public Item ( string text, DateTime date )
    {
        SomeText = text;
        SomeDate = date;
    }

    public string SomeText { get; private set; }
    public DateTime SomeDate { get; private set; }
}

// A simple viewmodel
class SomeViewModel : ViewModelBase
{
    public SomeViewModel ( )
    {
        Items.Add ( new Item ( "Item A", DateTime.MinValue ) );
        Items.Add ( new Item ( "Item B", DateTime.UtcNow   ) );
        Items.Add ( new Item ( "Item C", DateTime.MaxValue ) );
    }

    public ObservableCollection<Item> Items { get; }
     = new ObservableCollection<Item> ( );
}

Instead of giving it a key, we define our template by a type 我们没有给它一个密钥,而是通过一种类型来定义模板

<UserControl.Resources>

    <DataTemplate DataType="{x:Type local:Item}">

        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding SomeText}" />
            <DatePicker DisplayDate="{Binding SomeDate}" />
        </StackPanel>

    </DataTemplate>
</UserControl.Resources>

In both case, wpf will check the content's type and use our previous template we defined by type 在这两种情况下,wpf都会检查内容的类型并使用我们以前按类型定义的模板

<Grid>
    <ContentControl Content="{Binding Items[0]}" />

    <ItemsControl ItemsSource="{Binding Items}" />
</Grid>

You can use Resources , as follows: 您可以使用Resources ,如下所示:

<Window.Resources>
    <Style TargetType="TextBlock" x:Key="myTextBlock">
        <Setter Property="Text" Value="myText"/>
    </Style>
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBlock Style="{StaticResource myTextBlock}" Grid.Row="0"/>
    <TextBlock Style="{StaticResource myTextBlock}" Grid.Row="1"/>
    <TextBlock Style="{StaticResource myTextBlock}" Grid.Row="2"/>
</Grid>

as a possible solution you can define TextBox as non-shared resource. 作为一种可能的解决方案,您可以将TextBox定义为非共享资源。 and then display that resource in different ContentControls (via Content property) 然后在不同的ContentControls中显示该资源(通过Content属性)

<Window>
    <Window.Resources>
        <TextBox x:Key="SomeName" Text="{Binding SomeText}" x:Shared="False"/>
    </Window.Resources>

    <Grid>
        <ContentControl Content="{StaticResource SomeName}"/>
    </Grid>
    <Grid>
        <ContentControl Content="{StaticResource SomeName}"/>
    </Grid>
</Window>

in this example two TextBoxes will be generated, both bound to SomeText 在此示例中,将生成两个 TextBoxes,它们都绑定到SomeText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM