【问题标题】:usercontrol with button content binding带有按钮内容绑定的用户控件
【发布时间】:2011-08-20 08:15:18
【问题描述】:

我有一个想要重用的用户控件,但我似乎找不到更改按钮内容的方法。我已经尝试了我在这里搜索过的所有内容。如果有人能告诉我缺少什么,我将非常感激,因为我整天都在努力解决这个问题。这是我的代码:

    <UserControl x:Class="SamplePopupWPF.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
    <StackPanel>
        <Button x:Uid="btnLeft" Name="btnBindCmd" Height="23" Click="AddCommand" Content="{Binding ContentText, Mode=TwoWay}", DataContext="UserControl1"/>

    </StackPanel>
</Grid>

用户控制代码:

 public string ContentText
    {
        get { return (string) GetValue(ContentTextProperty);  }
        set { SetValue(ContentTextProperty, value); }
    }

  public static readonly DependencyProperty ContentTextProperty = DependencyProperty.Register("ContentText",     typeof (String),  typeof (UserControl1),new IPropertyMetadata(String.Empty,textChangedCallback));

    static void textChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

        UserControl1 c = (UserControl1) d;
        c.ContentText = e.NewValue as String;
   }

这里是使用控件的 Somepage:"

    <Grid >
            <user:UserControl1 x:Name="btnTEST" AddCommandClick="onBtnClick" ContentText="{Binding OtherCmd, ElementName=UserControl1}" />

    </Grid>

使用控件的 Somepage 代码:

public string OtherCmd
    {

        get { return _otherCmd; }
        set
        {
            _otherCmd = value;
            //if (this.PropertyChanged != null)
                NotifyProperty("OtherCmd");

        }
    }

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {
            OtherCmd = "helloTest";
    }

 public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyProperty(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

在某些时候,我没有使用上面放置的所有代码,但仍然无法正常工作。请帮忙。谢谢。

【问题讨论】:

    标签: wpf user-controls binding dependency-properties


    【解决方案1】:

    在您的 UserControl1.xaml.cs 中,在构造函数的 InitializeComponent(); 下写入 this.DataContext = this;

    这是因为您无法像在 UserControl1.xaml 中那样分配数据上下文。肯特展示了正确的方法。

    【讨论】:

    • 我尝试了上面的建议,但它不起作用:(。另外,请注意,在我的示例中,使用用户控件的是 Somepage.xaml,按钮内容文本将来自 Somepage .cs 代码。还有其他建议吗?谢谢!
    【解决方案2】:

    按钮绑定到 ContentText 但没有 DataContext,因此它是从其父级继承的。你可能想这样做:

    <UserControl
            x:Name="root"
            x:Class="SamplePopupWPF.UserControl1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Height="300"
            Width="300">
        <Grid DataContext="{Binding ElementName=root}">
            <StackPanel>
                <Button x:Uid="btnLeft" Name="btnBindCmd" Height="23" Click="AddCommand" Content="{Binding ContentText, Mode=TwoWay}", DataContext="UserControl1"/>
            </StackPanel>
        </Grid>
    </UserControl>
    

    请注意,根 Grid 将其 DataContext 设置为 UserControl 本身。因此,Button 中的Binding 将尝试解析UserControl 实例上的ContentText 属性。

    【讨论】:

      【解决方案3】:

      您只需要像这样使用 INOTIFYPROPERTYCHANGED 继承您的某个页面

      public class Subject :INotifyPropertyChanged
      {
      
      }
      

      【讨论】:

        猜你喜欢
        • 2016-01-15
        • 1970-01-01
        • 1970-01-01
        • 2018-01-29
        • 1970-01-01
        • 2017-03-18
        • 1970-01-01
        • 2011-05-22
        • 1970-01-01
        相关资源
        最近更新 更多