我有這種情況:從代碼C#編輯樣式WPF#
我在窗口中有一個fram。 我有一個頁面,將加載在框架中的代碼。 我在頁面中有一個網格,它具有代碼中的行和列定義。
我想populate網格行和列與按鈕(從矩形創建)。 我已經寫代碼:
樣式應用到每一個按鈕:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="rect" Style="{DynamicResource rectangle_style}" Cursor="Hand">
<Rectangle.Fill>
<ImageBrush ImageSource="Attempts\image.jpg" Stretch="UniformToFill"/>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content=""/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<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>
POPOLATE類代碼:
public void popolate(Grid grd)
{
for(int j = 1; j < 4; ++j)
{
for (int i = 1; i < 6; ++i)
{
Button btn = new Button();
btn.Margin= new Thickness(4,4,4,4);
grd.Children.Add(btn);
btn.SetValue(Grid.RowProperty, j);
btn.SetValue(Grid.ColumnProperty, i);
}
}
CALL類從MAIN:
公衆部分類MainWindow:窗口 {
Page1 page = new Page1();
Costruct costruttore = new Costruct();
public MainWindow()
{
this.InitializeComponent();
costruttore.popolate(page.LayoutRoot);
frame_first.NavigationService.Navigate(page);
// Insert code required on object creation below this point.
}
}
我想可以設置不同的圖像()是一個矩形爲從後面碼的每個按鈕。 我如何編寫這個必要的代碼?
請解釋一下,因爲我是WPF的一個主要人物!
costruct是一個類的名字! – Loris91 2013-03-20 17:49:49
你是否需要按鈕上的文字或圖像? – 2013-03-20 20:47:22
只有圖像,也許也是文字,但主要只有圖像! 你可以給一個解決方案嗎? – Loris91 2013-03-20 20:56:08