【问题标题】:Binding Items inside a DataTemplate with Items from another DataTemplate将 DataTemplate 中的项与另一个 DataTemplate 中的项绑定
【发布时间】:2011-03-05 20:35:12
【问题描述】:

我有两个数据模板(一个用于绘图[draw],另一个用于输入数据[data])我还有两个使用上述 DataTemplates 的 ContentControl。 我希望绑定两个 DataTemplate 的元素,以便当用户在数据表单 DateTemplate 中填写字段时,它也会自动更新绘图模板。

如何将draw DataTemplate中的元素与dataDataTemplate的元素绑定。 根本没有后端数据。用户从组合框中选取一个值,并根据在组合框中选择的值,我用相关的绘图和数据 DataTemplates 更新两个 ContentControl。用户在数据表单中填写相关字段,绘制模板根据一些业务规则绘制这些元素。

-----

    <DataTemplate x:Key="data">
        <Grid Grid.Row="0" Background="#FFFFFFFF" Name="DocumentRoot"  VerticalAlignment="Top">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition  Height="auto"/>
                <RowDefinition  Height="auto"/>
            </Grid.RowDefinitions>
            <Grid  Margin="10" VerticalAlignment="Top">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="200" />
                </Grid.ColumnDefinitions>

                <TextBlock Text="Heading Text" Grid.Row="1"/>
                <TextBlock Text="Ticket Text" Grid.Row="2"/>
               -----  
                <TextBox x:Name="txtHeading" Text="Heading Text" Grid.Row="1" Grid.Column="1"/>
                <TextBox x:Name="txtTicketText" Text="Ticket Text"  Grid.Row="2" Grid.Column="1"/>
                -----
            </Grid>


        </Grid>
    </DataTemplate>

 <ContentControl   Content="{Binding ElementName=cboTemplates, Path=SelectedItem.Name}"
                        ContentTemplateSelector="{StaticResource formTemplateSelector}">
                </ContentControl>

任何想法如何从不同的 DataTemplates 中绑定这两个元素?

提前致谢

【问题讨论】:

    标签: wpf xaml binding datatemplate


    【解决方案1】:

    为什么不将一个对象(具有Draw 属性和Data 属性的类)绑定到两个模板。当一个模板更改对象中的Data 属性时,您可以刷新对象中的Draw 属性,从而更新Draw 模板。


    更新


    例子:

    窗口内容

    <Grid>
        <StackPanel>
            <ContentControl DataContext="{Binding}">
                <ContentControl.Template>
                    <ControlTemplate>
                        <Rectangle Fill="{Binding Background}"
                                   Width="200"
                                   Height="200" />
                    </ControlTemplate>
                </ContentControl.Template>
            </ContentControl>
            <ContentControl DataContext="{Binding}">
                <ContentControl.Template>
                    <ControlTemplate>
                        <TextBox Text="{Binding ColorText}" />
                    </ControlTemplate>
                </ContentControl.Template>
            </ContentControl>
        </StackPanel>
    </Grid>
    

    代码背后

    public partial class MultiViewWindow : Window
    {
        public MultiViewWindow()
        {
            InitializeComponent();
    
            DataContext = new BackgroundInfo();
        }
    }
    
    public class BackgroundInfo : INotifyPropertyChanged
    {
        protected String _colorText;
        public String ColorText
        {
            get
            {
                return _colorText;
            }
            set
            {
                _colorText = value;
                RaisePropertyChanged("ColorText");
                RaisePropertyChanged("Background");
            }
        }
    
        public Brush Background
        {
            get
            {
                try
                {
                    return new SolidColorBrush((Color)ColorConverter.ConvertFromString(ColorText));
                }
                catch (Exception)
                {
                    return new SolidColorBrush(Colors.Transparent);
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler temp = PropertyChanged;
            if (temp != null)
            {
                temp(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    【讨论】:

    • 我确实尝试了上面的代码,但没有成功。我认为这是因为 DataContext 因为 ContentContorl 的 Binding 是与comboxBox。
    • 什么组合框?你是什​​么意思没有工作?它是否引发了绑定错误?
    • 我设法让它工作,但不确定这是否是最好/正确的做法。我所做的是为 DataContext 创建一个 staticResoruce,并明确地将其分配给我的 DataTemplates 的 Grid & Canvas。如果我设置 ControlContent 的 DataContext ,则 DataTemplates 不会从那里继承它,因为我相信 ContentControl 的 Content 属性设置为 ComboBox 用于动态确定 DataTemplate 。 ContentControl 的 Content 和 ContentTemplateSelector 属性可以链接到两个不同的资源吗?
    • 我认为您在这里感到困惑。我在编辑器中编写了这段代码,看到它正在运行,然后将其粘贴到这里。要完成这项工作,您需要做的是创建一个 WPF 应用程序项目,添加一个窗口 (WPF),将 ... 代码粘贴到窗口的内容中,转到该窗口的代码后面,粘贴那里有 C# 代码,转到 App.xaml,将启动文件名更改为此窗口并运行应用程序。当您在文本框中输入颜色名称并移除焦点时,它会更改其上方框中的颜色。
    • 我同意这段代码可以作为自己的代码运行,但在我的情况下它不能。如果 ContentControl 仅与 Windows 的 DataSource 绑定,那么它可以工作,但在我的情况下,CONtentCorntol 的绑定是与另一个控件(组合框)绑定的,因此可以拾取 DataTemplate。现在在 contentcontrol 的 DataTemplate 中,我需要将元素与其他一些 DataContext 绑定,而不是 ContentControl 的绑定。
    【解决方案2】:

    考虑创建类(名为 View Model)并将两个模板绑定到该类的单个实例(这是 Model-View-ViewModel 设计模式)。否则你可能会有非常复杂的绑定包含硬编码的逻辑树。

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 2016-09-07
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2011-12-01
      • 1970-01-01
      相关资源
      最近更新 更多