반응형
c# 코드로 Data Template를 빌드하려면 어떻게 해야 하나요?
winform interop의 드롭다운 목록을 작성하려고 하며, 코드에서 드롭다운 목록을 만들고 있습니다.단, 지정한 DataTemplate를 기반으로 바인드할 데이터를 얻는 데 문제가 있습니다.
제가 무엇을 빠뜨리고 있나요?
drpCreditCardNumberWpf = new ComboBox();
DataTemplate cardLayout = new DataTemplate {DataType = typeof (CreditCardPayment)};
StackPanel sp = new StackPanel
{
Orientation = System.Windows.Controls.Orientation.Vertical
};
TextBlock cardHolder = new TextBlock {ToolTip = "Card Holder Name"};
cardHolder.SetBinding(TextBlock.TextProperty, "BillToName");
sp.Children.Add(cardHolder);
TextBlock cardNumber = new TextBlock {ToolTip = "Credit Card Number"};
cardNumber.SetBinding(TextBlock.TextProperty, "SafeNumber");
sp.Children.Add(cardNumber);
TextBlock notes = new TextBlock {ToolTip = "Notes"};
notes.SetBinding(TextBlock.TextProperty, "Notes");
sp.Children.Add(notes);
cardLayout.Resources.Add(sp, null);
drpCreditCardNumberWpf.ItemTemplate = cardLayout;
이미 셋업을 완료했다고 가정합니다.ItemsSource
에 대해서drpCreditCardNumberWpf
...
//create the data template
DataTemplate cardLayout = new DataTemplate();
cardLayout.DataType = typeof(CreditCardPayment);
//set up the stack panel
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.Name = "myComboFactory";
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
//set up the card holder textblock
FrameworkElementFactory cardHolder = new FrameworkElementFactory(typeof(TextBlock));
cardHolder.SetBinding(TextBlock.TextProperty, new Binding("BillToName"));
cardHolder.SetValue(TextBlock.ToolTipProperty, "Card Holder Name");
spFactory.AppendChild(cardHolder);
//set up the card number textblock
FrameworkElementFactory cardNumber = new FrameworkElementFactory(typeof(TextBlock));
cardNumber.SetBinding(TextBlock.TextProperty, new Binding("SafeNumber"));
cardNumber.SetValue(TextBlock.ToolTipProperty, "Credit Card Number");
spFactory.AppendChild(cardNumber);
//set up the notes textblock
FrameworkElementFactory notes = new FrameworkElementFactory(typeof(TextBlock));
notes.SetBinding(TextBlock.TextProperty, new Binding("Notes"));
notes.SetValue(TextBlock.ToolTipProperty, "Notes");
spFactory.AppendChild(notes);
//set the visual tree of the data template
cardLayout.VisualTree = spFactory;
//set the item template to be our shiny new data template
drpCreditCardNumberWpf.ItemTemplate = cardLayout;
내가 설정한 것과 같은 방법으로 사용할 수 있습니다.ToolTip
에서TextBlock
s: 여백 등의 기타 속성을 설정합니다.
풀버전
var ms = new MemoryStream(Encoding.UTF8.GetBytes(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:c=""clr-namespace:MyApp.Converters;assembly=MyApp"">
<DataTemplate.Resources>
<c:MyConverter x:Key=""MyConverter""/>
</DataTemplate.Resources>
<TextBlock Text=""{Binding ., Converter={StaticResource MyConverter}}""/>
</DataTemplate>"));
var template = (DataTemplate)XamlReader.Load(ms);
var cb = new ComboBox { };
//Set the data template
cb.ItemTemplate = template;
음, 물론 다른 방법이 있긴 하지만, 만약 그런 것들이 싫다면 정말 좋아할 거예요.
그리고 내추럴코드에 사소한 변경을 가하는 것 같습니다.즉, a를 선언하고 컨트롤한 후 1개만 사용하여를 호출합니다.
간단한 데모 코드(F#):
let buildView()=StackPanel()
//Build it with natural code
type MyView()=inherit UserControl(Content=buildView())
let factory=FrameworkElementFactory(typeof<MyView>)
let template=DataTemplate(VisualTree=factory)
let list=ItemsControl(ItemsSource=makeData(),ItemTemplate=template)
언급URL : https://stackoverflow.com/questions/248362/how-do-i-build-a-datatemplate-in-c-sharp-code
반응형
'programing' 카테고리의 다른 글
정적 상수 문자열(클래스 멤버) (0) | 2023.04.22 |
---|---|
sql 기본 키 및 인덱스 (0) | 2023.04.17 |
Swift 어레이 할당에 일관성이 없는 이유(레퍼런스나 딥 카피 모두)가 있습니까? (0) | 2023.04.17 |
셸 스크립트의 YYY-MM-DD 형식 날짜 (0) | 2023.04.17 |
bash에서 단일 명령을 사용하여 셸 변수에 기본값 할당 (0) | 2023.04.17 |