内容控件绑定不起作用

时间:2013-05-27 10:57:42

标签: wpf contentcontrol

当我像下面那样设置内容控件的内容时,内容中元素的绑定会中断。​​

我在一个类的属性中给出了一个内容,并将该属性设置为内容控件的内容。

[Xmal]
<Grid>
    <Button HorizontalAlignment="Center"
            VerticalAlignment="Top"
            Click="Button_Click_1"
            Content="Click" />
    <local:MyTile x:Name="mytile">
        <local:MyTile.TileViewContent>
            <StackPanel>
                <TextBox x:Name="text"
                         Background="Red"
                         Text="MyText" />
                <TextBox Text="{Binding ElementName=text, Path=Text,Mode=TwoWay}" />
            </StackPanel>
        </local:MyTile.TileViewContent>
    </local:MyTile>
    <ContentControl x:Name="contentcontrol" />
</Grid>
[C#]

    public partial class MainWindow : Window
    {
      public MainWindow()
      {
        InitializeComponent();
      }

      private void Button_Click_1(object sender, RoutedEventArgs e)
      {
       contentcontrol.Content =  mytile.TileViewContent;
      }
    }         

    public class MyTile:Control
    {
      public FrameworkElement TileViewContent
      {
        get { return (FrameworkElement)GetValue(TileViewContentProperty); }
        set { SetValue(TileViewContentProperty, value); }
      }

      public static readonly DependencyProperty TileViewContentProperty =
        DependencyProperty.RegisterAttached("TileViewContent", typeof(FrameworkElement), typeof(MyTile), new PropertyMetadata(null));
     }

当我设置内容绑定不起作用时。请帮忙

1 个答案:

答案 0 :(得分:0)

如果您想简单地绑定是有效的,不一定是通过 ContentControl ,请使用样式作为您的元素:

<Window.Resources>
       <Style x:Key="MyTemplateForMyControl" TargetType="{x:Type local:MyTile}">
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type local:MyTile}">                        
                       <StackPanel>
                           <TextBox x:Name="MyTextBox" Text="MyText" Background="Red" />
                           <TextBox Text="{Binding ElementName=MyTextBox, Path=Text}" />
                       </StackPanel>                        
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
       </Style>
</Window.Resources>

<Grid>
    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Click" Click="Button_Click_1" />
    <local:MyTile x:Name="MyTile" />         
</Grid>

在代码中,我们为控件设置样式

private void Button_Click_1(object sender, RoutedEventArgs e)
{   
    MyTile.Style = this.Resources["MyTemplateForMyControl"] as Style;
}

如果有必要使用 ContentControl ,我可以推荐而不是控制使用 DataTemplate

<Window.Resources>
    <DataTemplate DataType="{x:Type local:MyDataForTemplate}">
        <StackPanel>
            <TextBox x:Name="MyTextBox" Text="{Binding TextBoxContent}" Background="Red" />
            <TextBox Text="{Binding ElementName=MyTextBox, Path=Text}" />
        </StackPanel>
    </DataTemplate>

    <!-- Some data -->
    <local:MyDataForTemplate x:Key="MyDataForTile" TextBoxContent="MyText" />
</Window.Resources> 

<Grid>
    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Click" Click="Button_Click_1" />
    <ContentControl Name="TileContentControl" />
</Grid>

模板会有一些数据:

public class MyDataForTemplate
{
    string textBoxContent = "";

    /// <summary>
    /// Text for TextBox
    /// </summary>
    public string TextBoxContent
    {
        get
        {
            return textBoxContent;
        }

        set
        {
            textBoxContent = value;
        }
    }
}

在代码中,我们设置模板的数据:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    TileContentControl.Content = this.Resources["MyDataForTile"] as MyDataForTemplate;
}