UserControl
은 프로그래밍 방식으로 스택 패널에 표시하고 싶습니다. .Silverlight UserControl이 표시되지 않습니다.
내가 할 때, UC는 화면에 표시되지 않습니다. 도구 상자에서 하나의 인스턴스를 드래그하면 제대로 작동합니다.
사용자 정의 컨트롤은 XAML은 코드 숨김
<UserControl x:Class="MYProj.Controls.SpecialNumberOption"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="200">
<Viewbox>
<Grid x:Name="LayoutRoot" Background="White" Width="200" Height="300">
<Button x:Name="buttonMe" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="180" Height="150" Style="{StaticResource NumberButtonStyle}" Click="buttonMe_Click"/>
<TextBlock x:Name="subText" HorizontalAlignment="Left" Margin="10,165,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="125" Width="180" FontStyle="Italic" TextAlignment="Center"/>
</Grid>
</Viewbox>
</UserControl>
입니다
public partial class SpecialNumberOption : UserControl
{
public event RoutedEventHandler Click;
public SpecialNumberOption()
{
InitializeComponent();
this.applyStyle();
}
public SpecialNumberOption(SurveyQuestionOption option)
{
this.buttonMe.Content = option.Text;
this.subText.Text = option.SubText;
this.applyStyle();
}
private void applyStyle()
{
this.buttonMe.FontSize = 26;
this.buttonMe.Background = standardBackground;
this.buttonMe.Foreground = standardForecolor;
}
///Raise the event to the outside
private void buttonMe_Click(object sender, RoutedEventArgs e)
{
Click(this, e);
}
}
구현
이 임 제어
,369을 추가하는 방법입니다foreach (var y in x.Options)
{
//Create new instance from An object
var r = new SpecialNumberOption(y);
// Set visibility
r.Visibility = System.Windows.Visibility.Visible;
r.IsEnabled = false;
//Assign the event handler
r.Click += r_Click;
//This is my stackpanel
listOptions.Children.Add(r);
....
}
//Handle the click event
void r_Click(object sender, RoutedEventArgs e)
{
SpecialNumberOption o = (SpecialNumberOption)e.OriginalSource;
....
}
내가 대체 생성자를 사용할 때 작동을 중단 할 때 이것이 발견
업데이트. 기본 생성자를 사용해야합니다. 이게 정상인가? 여기 정확성에 대한 게시 된 모든 코드를 확인하지만하지 않은
r.IsEnabled = false; 사실대로 해보십시오. – ray
그래, 그걸 시도해도 안된다. IsEnabled는 컨트롤이 상호 작용하도록 허용하거나 금지해야한다. 시각적으로 나타나야합니다. – IEnumerable
대신 foreach 루프에서 하나의 컨트롤을 동적으로 추가해보십시오. – ray