2017-07-03 62 views
-1

我要找的示例代碼簡潔片斷,做以下設置組件用戶控件的屬性:如何在WPF XAML

從更高的層次UserControl,我想改變一個對象的屬性(比如說)經由XAML在子UserControl內。

例如,假設我有一個UserControl,名爲Widget,其中包含GridButton s。每個Button都有不同的背景和邊框顏色。然後我想要一個名爲WidgetPanelUserControl,它保留GridWidgets

對於WidgetPanel內的每個Widget定義,我想是能夠設置BorderBrush和每個單獨的按鈕(命名button0button1button2分別)通過XAML屬性的Background。我還想以編程方式在WidgetPanel.xaml.cs後面的代碼中更改事件的這些值。

這裏是XAML和後面爲每個對象的代碼:

XAML爲Widget

<UserControl x:Class="WpfApp1.Widget" 
      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" 
      xmlns:local="clr-namespace:WpfApp1" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Button BorderBrush="Black" BorderThickness="4" Background="#FF249AA6" Grid.Row="0"/> 
     <Button BorderBrush="Blue" BorderThickness="4" Background="#FFFF0046" Grid.Row="1"/> 
     <Button BorderBrush="Orange" BorderThickness="4" Background="Blue" Grid.Row="2"/> 
    </Grid> 
</UserControl> 

代碼後面爲Widget

using System.Windows.Controls; 

namespace WpfApp1 
{ 
    public partial class Widget : UserControl 
    { 
     public Widget() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

XAML爲WidgetPanel

<UserControl x:Class="WpfApp1.WidgetPanel" 
      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" 
      xmlns:local="clr-namespace:WpfApp1" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <local:Widget Grid.Column="0"/> 
     <local:Widget Grid.Column="1"/> 
    </Grid> 
</UserControl> 
背後

代碼WidgetPanel

using System.Windows.Controls; 

namespace WpfApp1 
{ 
    public partial class WidgetPanel : UserControl 
    { 
     public WidgetPanel() 
     { 
      InitializeComponent(); 
     } 
    } 
}  
+0

在'WidgetPanel'和'Widget'中定義了一些屬性。 –

+0

如果你可以提供一個答案,那麼我將會非常感激。 –

+0

小部件中的按鈕列表是否已預定義?如果是的話,你可以在'Widget'類(像FirstButtonBorderBrush和FirstButtonBackground)中創建所需的依賴屬性,並按照@LeiYang的說法將按鈕的屬性綁定到它們。 – Maxim

回答

1

Widget類中定義一組影響內部按鈕樣式的屬性。例如,對於BorderBrush

public partial class Widget : UserControl 
{ 
    public Widget() 
    { 
     InitializeComponent(); 
    } 

    // BorderBrush for first button 

    public static readonly DependencyProperty FirstButtonBorderBrushProperty = 
     DependencyProperty.Register("FirstButtonBorderBrush", 
            typeof(Brush), 
            typeof(Widget)); 

    public Brush FirstButtonBorderBrush 
    { 
     get { return (Brush)GetValue(FirstButtonBorderBrushProperty); } 
     set { SetValue(FirstButtonBorderBrushProperty, value); } 
    } 

    // ... repeat for other buttons 
} 

在XAML的Widget

<Button BorderBrush="{Binding Path=FirstButtonBorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/> 

WidgetPanel XAML:

<local:Widget x:Name="firstWidget" 
       FirstButtonBorderBrush="Red"/> 

當然你也可以從代碼隱藏設置該屬性WidgetPanel

firstWidget.FirstButtonBorderBrush = new SolidColorBrush(Colors.Red); 
+0

今天我會試試這個謝謝。如果有效,我會接受。但哇這樣簡單的事情的複雜語法對嗎? –

+0

你會習慣它:) – Maxim

+0

好的。我想我明白了。我們正在做的不是爲實際對象設置get/set,比如我正在修改的Button.Background。相反,我們正在做的是將一個小部件的XAML關鍵字「Background」重定向到一個Brush對象的setter。那個聽起來是對的嗎?唷。大聲笑我希望有一個很好的WPF綜合教程...這很酷,當你知道這些技巧時,工作流程非常棒......但是有時候很難找到答案。感謝您的幫助@Maxim –

0

我不知道是否下面的想法工作。嘗試下面的代碼,它可能會解決您的查詢。

絲如下WidgetPanel.xaml.cs加載的用戶控件WidgetPanel網格內的焦點項目,

grid.Focused += Grid_Focused; //where grid is the name of Grid loaded inside a WidgetPanel user control. 

private void Grid_Focused(object sender, EventArgs e) 
{ 
    Grid grid = sender as Grid; 
    //here you can get the children of grid i.e, you can get the usercontrols in Widget.xaml 
    //From the user control get its children. So you can easily get the buttons here and change the respective properties. 
} 

我剛纔跟我的看法。我沒有檢查上面的代碼。願它幫助你。

+0

這不是我的問題的答案,但稍後可能會有用。謝謝。 –