【问题标题】:Custom Button with Binding带绑定的自定义按钮
【发布时间】:2014-03-07 10:31:02
【问题描述】:

我想在 WPF 中创建一个自定义按钮,所以我写了这段代码:

<UserControl x:Class="RiabilitazioneCognitiva.ButtonPersonalizzati"
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Width="Auto" Height="Auto">
    <FrameworkElement.Resources>
        <ResourceDictionary Source="GlassButton.xaml" />
    </FrameworkElement.Resources>
        <Button  x:Name="pippo" Style="{DynamicResource GlassButton}" 
        Click="button_Click">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FFFFFFFF"  />

        </StackPanel>
    </Button>


</UserControl>

现在我在我的页面中插入这个按钮,所以我试试这个代码:

<Window xmlns:RiabilitazioneCognitiva="clr-namespace:RiabilitazioneCognitiva"  x:Name="framePrincipale" x:Class="RiabilitazioneCognitiva.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:RiabilitazioneCognitiva"
        Title="Stop" 
        Height="{Binding}" 
        Width="{Binding}" 
        Background="White" 
        WindowStartupLocation="CenterScreen" 
        WindowStyle="None"
        WindowState="Maximized"
        ResizeMode="NoResize">
</Window>

<Grid>
   <local:ButtonPersonalizzati />
</Grid>

找到了,但是如果我插入这个我看不到按钮

<local:ButtonPersonalizzati x:Text="pp" >

我们能帮帮我吗?

谢谢

PS:在 ButtonPersonalizzati.cs 我有这个

public string Text { 
   get{return Text;}
}

【问题讨论】:

  • 什么不起作用?编译错误?运行时错误?有什么例外吗?您需要在此处添加更多详细信息。
  • 我没有错误、异常或运行时错误,但在我的窗口中没有看到 CustomBottm
  • 这将抛出 SO 异常,因为循环引用 - public string Text { get{return Text;}}
  • 好的,我更改我的代码,公共字符串 Text {get;set;} 。所以在 XAML 文件中我尝试了这个 但我没有看到按钮上的字符串 Pippo

标签: c# wpf button custom-controls


【解决方案1】:

您尚未设置数据上下文。有多种方法可以解决这个问题,但我的典型做法是这样做:

<UserControl ...
             x:Name="ucThis">
         ...
        <TextBlock Text="{Binding ElementName=ucThis Path=Text}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FFFFFFFF"  />
         ...
</UserControl>

【讨论】:

    【解决方案2】:

    关闭标签local:ButtonPersonalizzati

    <local:ButtonPersonalizzati x:Name="pp" local:Text="Pippo" />
    

    【讨论】:

      【解决方案3】:

      Text 定义为 DependencyProperty,以便您可以绑定到它:

      public string Text
      {
          get { return (string)GetValue(TextProperty); }
          set { SetValue(TextProperty, value); }
      }
      public static readonly DependencyProperty TextProperty =
          DependencyProperty.Register("Text", typeof(string), typeof(ButtonPersonalizzati), new UIPropertyMetadata(""));
      

      您还应该在类的构造函数中设置 DataContext:

      this.DataContext=this;
      

      【讨论】:

      • 有效,但没用:它不会反映属性的变化
      猜你喜欢
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 2020-09-26
      相关资源
      最近更新 更多