繁体   English   中英

如何在WPF中的按钮中动态设置多个内容

[英]How Do I Set Multiple Content Dynamically in a Button in WPF

我创建了以下xaml:

 <Button x:Name="PriceButton">
  <Button.Template>
    <ControlTemplate>
      <Border x:Name="ButtonBorder"
                        CornerRadius="2"
                        Background="{StaticResource DarkReflectionBrush}"
                        BorderBrush="Black"
                        BorderThickness="1" HorizontalAlignment="Stretch">
          <ContentPresenter 
            VerticalAlignment="Center"
            HorizontalAlignment="Left">
          <ContentPresenter.Content>
            <Grid x:Name="ContentGrid" HorizontalAlignment="Left" >
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="15*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition  Width="15*"  />
              </Grid.ColumnDefinitions>
              <TextBlock x:Name="Price" Grid.Column="0" FontFamily="Calibri" FontSize="8" 
                         VerticalAlignment="Bottom" Text="{Binding Path=PriceText}"
                         Foreground="{Binding Path=PriceColor}" ></TextBlock>
              <TextBlock x:Name="Main" Grid.Column="1" FontFamily="Calibri" FontSize="14"
                         Text="{Binding Path=MainText}"
                         Foreground="{Binding Path=MainTextColor}" />
              <TextBlock x:Name="Vol" Grid.Column="2" FontFamily="Calibri" FontSize="8" 
                          VerticalAlignment="Bottom" Text="{Binding Path=VolatilityText}"
                         Foreground="{Binding Path=VolatilityColor}" />
            </Grid>
          </ContentPresenter.Content>
        </ContentPresenter>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="Button.IsPressed" Value="true">
          <Setter TargetName="ButtonBorder" Property="Background" Value="{StaticResource PressedBrush}"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
          <Setter Property="Opacity" Value="0.5" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Button.Template>
</Button>

这是背后的代码:

  public static readonly DependencyProperty PriceTextProperty =
  DependencyProperty.Register(
    "PriceText",
    typeof (string),
    typeof (LivePriceVolButton),
    new FrameworkPropertyMetadata(string.Empty, OnPriceTextChanged));

public string PriceText {
  get {
    return (string)GetValue(PriceTextProperty);
  }
  set {
    SetValue(PriceTextProperty, value);
  }
}

public static readonly DependencyProperty PriceColorProperty =
  DependencyProperty.Register(
    "PriceColor",
    typeof (Color),
    typeof (LivePriceVolButton),
    new FrameworkPropertyMetadata(Colors.White));

public Color PriceColor {
  get {
    return (Color) GetValue(PriceColorProperty);
  }
  set {
    SetValue(PriceColorProperty, value);
  }
}

public static readonly DependencyProperty MainTextProperty =
  DependencyProperty.Register(
    "MainText",
    typeof (string),
    typeof (LivePriceVolButton),
    new FrameworkPropertyMetadata(string.Empty));

public string MainText {
  get {
    return (string) GetValue(MainTextProperty);
  }
  set {
    SetValue(MainTextProperty, value);
  }
}

public static readonly DependencyProperty MainTextColorProperty =
  DependencyProperty.Register(
    "MainTextColor",
    typeof(Color),
    typeof(LivePriceVolButton),
    new FrameworkPropertyMetadata(Colors.White));

public Color MainTextColor {
  get {
    return (Color) GetValue(MainTextColorProperty);
  }
  set {
    SetValue(MainTextColorProperty, value);
  }
}

public static readonly DependencyProperty VolatilityTextProperty =
  DependencyProperty.Register(
    "VolatilityText",
    typeof(string),
    typeof(LivePriceVolButton),
    new FrameworkPropertyMetadata(string.Empty));

public string VolatilityText {
  get {
    return (string) GetValue(VolatilityTextProperty);
  }
  set {
    SetValue(VolatilityTextProperty, value);
  }
}

public static readonly DependencyProperty VolatilityColorProperty =
  DependencyProperty.Register(
    "VolatilityColor",
    typeof(Color),
    typeof(LivePriceVolButton),
    new FrameworkPropertyMetadata(Colors.White));

public Color VolatilityColor {
  get {
    return (Color) GetValue(VolatilityColorProperty);
  }
  set {
    SetValue(VolatilityColorProperty, value);
  }
}

当我将用户控件插入这样的表单时

 <my:LivePriceVolButton Margin="43.03,0,0,32" x:Name="livePriceVolButton1" 
                       xmlns:my="clr-namespace:MultiTextBlockButton" HorizontalAlignment="Left" 
                       Width="91.703" Height="30" VerticalAlignment="Bottom" 
                       MainText="MID" MainTextColor="LightBlue" PriceColor="Red" PriceText="1234.56"
                       VolatilityText="12.2%" VolatilityColor="Aqua" />

我根本没有在按钮中看到任何东西。 有任何想法吗?

谢谢

您必须将Button的DataContext设置为与父UserControl相等,才能使绑定起作用。 尝试这样的事情:

<UserControl x:Name="uc" ...>

  <Button x:Name="PriceButton" DataContext="{Binding ElementName=uc}">
    <!--Other code here...-->
  </Button>

</UserControl>

我还看到您正在使用“颜色”作为某些DependencyProperties的类型。 我建议您将其更改为“画笔”。 否则相关的绑定(例如,Foreground =“ {Binding VolatilityColor}”)将不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM