【问题标题】:How to make two independent instance of a ControlTemplate In a window?如何在一个窗口中创建一个 ControlTemplate 的两个独立实例?
【发布时间】:2012-03-27 15:07:49
【问题描述】:

我有一个带有文本框和按钮的控件模板,该按钮打开一个子表单以选择某些内容并在文本框中显示所选项目,如下所示:

    <Window.Resources>
    <ControlTemplate x:Key="CreateParam">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="3*"/>
            </Grid.ColumnDefinitions>
            <Button Content="select" Command="{Binding ShowSpecItemViewommand}"  Grid.Column="0" Margin="2"/>
            <TextBox Margin="2" Text="{Binding Param}" Grid.Row="0" Grid.Column="1"/>
            <TextBlock Margin="5" Text="patameter" Grid.Row="0" Grid.Column="2"/>
        </Grid>
    </ControlTemplate>
    </Window.Resources>

我在视图模型中有这样的属性:

 public string param;
    public string Param
    {
        get
        {
            return param;
        }
        set
        {
            param = value;
            RaisePropertyChanged("Param");
        }
    }

现在我想在一个窗口中创建该控件的两个独立实例,但是当我为第一个实例选择一个值时,它们都已更改。我应该定义两个属性吗?如何将它们绑定到控制模板? 我不确定每个人都能理解我的意思,所以我希望有人编辑我的问题:)

【问题讨论】:

    标签: binding mvvm mvvm-light controltemplate


    【解决方案1】:

    您如何使用控制模板?您将此模板附加到哪个控件?它是您拥有的自定义控件的模板吗?它是已知控件的模板吗?

    如何为控件模板实例化 DataContext?

    虽然您可以使用 ControlTemplate(和自定义控件)来实现您想要的,并且如果您的对象有很多(即多于两个,甚至全部)实例,那么 ControlTemplate 可能是正确的范例,您将最好使用 DataTemplate 或 UserControl。实现您想要的方法不止一种,但下面的代码被认为是“规范”的解决方案:

    说 Param 是 MyVM 对象的一个​​属性。那么你的 XAML 文件应该是:

    <Window
        x:Class="SO.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:so="clr-namespace:SO"
        Height="200" Width="350"
        Title="SO Sample"
        >
        <Window.Resources>
            <DataTemplate DataType="{x:Type so:MyVM}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="3*"/>
                    </Grid.ColumnDefinitions>
                    <Button Content="select" Command="{Binding ShowSpecItemViewommand}"  Grid.Column="0" Margin="2"/>
                    <TextBox Margin="2" Text="{Binding Param}" Grid.Row="0" Grid.Column="1"/>
                    <TextBlock Margin="5" Text="patameter" Grid.Row="0" Grid.Column="2"/>
                </Grid>
            </DataTemplate>
        </Window.Resources>
    
        <StackPanel>
            <ContentControl>
                <so:MyVM Param="1234" />
            </ContentControl>
            <ContentControl>
                <so:MyVM Param="5678" />
            </ContentControl>        
        </StackPanel>
    
    </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      相关资源
      最近更新 更多