2017-06-21 5 views
0

によってそれらを作成した後、私は私が
との国境のBackgroundBorderBrushのためのデータバインディングを作成したい
WPFのC#のコードでボーダーとラベルを作成しましたここではラベル、プログラムコード

Backgroundは、私がその

のようなXAMLでボタンのためにしたコード

void creatlbl() 
{ 
    Border b = new Border(); 
    b.Name = "b11"; 
    b.Margin = new Thickness(300,17,0,419); 
    b.Height = 32; 
    b.CornerRadius = new CornerRadius(5); 
    b.Width = 181; 
    b.BorderThickness = new Thickness(2); 
    b.HorizontalAlignment = HorizontalAlignment.Left; 
    lgingrd.Children.Add(b); // 
    Label l = new Label(); 
    l.Name = "l111"; 
    l.Content = "l111"; 
    l.Height = 28; 
    l.Width = 177; 
    l.Foreground = (Brush)bc.ConvertFrom("#FF346D80"); 
    l.FontSize = 20; 
    l.Background = (Brush)bc.ConvertFrom("#FF9AB426"); 
    l.HorizontalContentAlignment = HorizontalAlignment.Center; 
    l.Padding = new Thickness(0,0,0,0); 
    l.Visibility = Visibility.Visible; 
    b.Child = l; 
    // here i want to set binding for border 
    // the background and borderbrush of border equal to the background of label 
} 

同じものです

<Border x:Name="brdbt" Margin="120,58,0,378" 
    BorderBrush="{Binding Background, ElementName=bt}" <!--this is the binding which i want--> 
    Height="32" 
    CornerRadius="5" 
    Width="181" 
    BorderThickness="2" 
    HorizontalAlignment="Left" Background="{Binding Background, ElementName=bt}"> 
    <Button x:Name="bt" Content="btntxt" HorizontalAlignment="Left" VerticalAlignment="Top" Height="28" Width="177" Click="bt_click" BorderBrush="{x:Null}" Foreground="#FFE8EEF0" FontSize="20" Padding="1,-1,1,1" MouseEnter="bt_mcentr" MouseLeave="bt_mclv" Background="#FFE62828"/> 
</Border> 

上記のxaml型のバインドを境界線とラベルのC#コードで行う方法はありますか?

答えて

2

パスをソースとしてラベルlを使用して「背景」(new Binding("Background") { Source = l })をBindingオブジェクトを作成し、プロパティをターゲットに結合することを割り当てる:Border.BackgroundとBorder.BorderBrush

​​
-1

をあなたはそれが簡単になるだろう知っていますxamlにこのコードブロック全体を作成します。 DataTemplateでkeyを使ってラップしてください。あなたのコードで 、のContentPresenterを作成し、コンテンツとして、それをあなたのデータを与え、 そしてそれはContentTemplate値だとして、それをあなたのDataTemplateを与える:

ContentPresenter cp = new ContentPresenter(); 
cp.Content = (?) 
cp.ContentTemplate = Application.Current.Resources.Find("DataTemplateKey") as DataTemplate; 
関連する問題