你如何在WPF中使用StackPanel.FindName?

时间:2014-10-06 04:35:41

标签: c# wpf

我有一个堆栈面板,里面有一组四个图像按钮。当我尝试按图像按钮的名称搜索时,它返回null。我的代码出了什么问题?

XAML:

<StackPanel x:Name="swatchClearStackPanel" Orientation="Vertical" Grid.Column="1" Grid.Row="2" Margin="0,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Bottom">
     <imgbutton:ImageButton x:Name="ClearSwatchColor1" Grid.Row="0" Grid.Column="2" Style="{DynamicResource ButtonClearSwatchColor}" Margin="5,8,5,5" HorizontalAlignment="Left"  Visibility="Hidden" Click="ClearSwatchColor_Click"  />
     <imgbutton:ImageButton x:Name="ClearSwatchColor2"  Grid.Row="0" Grid.Column="2" Style="{DynamicResource ButtonClearSwatchColor}"  Margin="5,18,5,5" HorizontalAlignment="Left"  Visibility="Hidden" Click="ClearSwatchColor_Click"/>
     <imgbutton:ImageButton x:Name="ClearSwatchColor3"  Grid.Row="0" Grid.Column="2" Style="{DynamicResource ButtonClearSwatchColor}"  Margin="5,18,5,5" HorizontalAlignment="Left"  Visibility="Hidden" Click="ClearSwatchColor_Click"/>
     <imgbutton:ImageButton x:Name="ClearSwatchColor4"  Grid.Row="0" Grid.Column="2" Style="{DynamicResource ButtonClearSwatchColor}" Margin="5,20,5,5" HorizontalAlignment="Left"  Visibility="Hidden" Click="ClearSwatchColor_Click"/>
</StackPanel>

和C#: 编辑:注意:按钮名称是硬编码的,表示无法找到它,最终结果将是一个变量,作为多个按钮将引用的一种方法。在加载UI之后触发此代码。 (用户单击一个按钮,此代码运行。相同的窗口,相同的一切。)

var swatchClearButton = swatchClearStackPanel.FindName("ClearSwatchColor1");

0 个答案:

没有答案