2017-12-05 9 views
0

私はExpression Blendを使用してボタンをデザインしていました。新しいボタンを作成し、その中のすべてを削除してボタンのテンプレートを編集しました。次に、グリッドを追加して「グリッド」という名前を付けました。グリッドの中で私はテキストブロックを追加し、それを "textBlock"と呼んだ。次に、アプリケーションの下にテンプレートを保存しました。実行時にWPFボタンを編集するには

これはapp.xamlファイルのコンテンツです。

<Style x:Key="CustomButtonStyle" TargetType="{x:Type Button}"> 
     <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> 
     <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Grid x:Name="grid" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="90"> 
         <TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Margin="5,5,0,0" Height="25" Width="75" FontSize="17.333"/> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsDefaulted" Value="true"/> 
         <Trigger Property="IsMouseOver" Value="true"/> 
         <Trigger Property="IsPressed" Value="true"/> 
         <Trigger Property="IsEnabled" Value="false"/> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

は基本的に私は何をしようとしていますことは、実行時にボタンを作成ボタンにこのスタイルを適用すると、ボタンのテンプレートに行く、とのテンプレートにテキストブロックのテキストプロパティを変更することですボタン。

まず、実行時にボタンを作成するのではなく、コンパイル時にボタンを作成して「customButton」という名前を付けました。次に、ボタンのテンプレートのテキストブロックのテキストプロパティを編集しようとしましたが、例外が発生しました。

TextBlock tb = this.customButton.Template.FindName("textBlock", customButton) as TextBlock; 
tb.Text = "ASDDDQWEQWEQWE"; 

ありがとうございました!

+0

ユーザがテンプレートを簡単に変更する必要がある場合、それを逆アセンブルしなければならないテンプレートデフォルトのボタンテンプレートは 'ContentPresenter'を使って任意のContentを表示します(これはテキストではありません!!)。私は同じことをお勧めします – ASh

+0

実行時にテキストボックスの内容を動的に変更したい場合は、 'Text'フィールドをプロパティにバインドし、コード内のプロパティを変更することを検討してください。 – Jack

+0

会社名、画像、株価上昇率などの会社の株価情報を1つのボタンで表すためのボタンを作成したいと考えています。したがって、実行時には、外部APIから上位10個の在庫を取得し、実行時に情報とともに10個のボタンを作成し、それをメインウィンドウのスタックパネルに追加します。 –

答えて

1

私は 会社名、画像、及び全て1つの つのボタンで株価の%増のような会社の株式情報を表すためにボタンを作成します。だから、実行時に 外部APIから上位10個の株式を取得し、実行時に10個のボタンを作成し、メインウィンドウのスタックパネルに を追加します。

このようなシナリオでは、ListBoxとItemTempateを使用する必要があります。 次のコードを試してください。

<ListBox x:Name="lstBox"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Button> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding CompanyName}" Margin="0,0,10,0"/> 
         <TextBlock Text="{Binding Percent}" /> 
        </StackPanel> 
       </Button> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     ObservableCollection<CompanyModel> companies = new ObservableCollection<CompanyModel>(); 
     for (int i = 0; i < 10; i++) 
     { 
      CompanyModel companyModel = new CompanyModel() 
      { 
       Percent = i, 
       CompanyName = "Name" + i 
      }; 
      companies.Add(companyModel); 
     } 
     lstBox.ItemsSource = companies; 
    } 
} 

class CompanyModel 
    { 
     public int Percent { get; set; } 
     public string CompanyName { get; set; } 
    } 
1

あなたが持っているものと同様、ボタンテンプレートを作成し、あなたの項目ごとにコントロールを追加します。

<ControlTemplate TargetType="{x:Type Button}"> 
       <StackPanel> 
        <Label Content="{Binding CompanyName}" /> 
        <Image Source="{Binding Image}" /> 
        <Label Content="{Binding StockChange}" /> 
       </StackPanel> 
</ControlTemplate> 

それとも、ただボタンのコンテンツに追加することができます:

<Button> 
    <Button.Content> 
     <StackPanel> 
      <Label Content="{Binding CompanyName}" /> 
      <Image Source="{Binding Image}" /> 
      <Label Content="{Binding StockChange}" /> 
     </StackPanel> 
    </Button.Content> 
</Button 

作成を作成

public class CompanyInfo 
{ 
    public string CompanyName; 
    public ImageSource Image; 
    public string StockChange; 
} 

:あなたのAPIからのデータを保持するためclassあなたのAPIデータから作成されたCompanyInfoオブジェクトを保持するための10のコードビハインドかのviewmodel中:

public ObservableCollection<CompanyInfo> CompanyInfoList = new ObservableCollection<CompanyInfo>(); 

のデータのリストからボタンを作成するItemsControlを作成します。

<ItemsControl ItemsSource="{Binding Path=CompanyInfoList}"> 

    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ItemsControl.ItemTemplate> 
     <DataTemplate> 

      //Button with Bindings Goes Here 

     </DataTemplate> 
    </ItemsControl.ItemTemplate> 

</ItemsControl> 

そして、あなたの叔父をボブス。どのように動作するのかをよりよく理解するために、これらのことのいくつかを調べることをお勧めします。

関連する問題