반응형
C # 코드에서 DataTemplate을 어떻게 빌드합니까?
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
etc를 설정했다고 가정합니다 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;
글쎄, 우리는 여전히 다른 방법이 있습니다. 당신이 그런 FrameworkElementFactory
것들을 싫어한다면 당신은 정말 그것을 좋아할 것입니다.
그리고 나는 그것이 자연 코드를 약간 변경한다고 생각합니다. 즉, a를 선언하고 UserControl
컨트롤을 넣은 다음 하나만 사용 FrameworkElementFactory
하여 UserControl
.
간단한 데모 코드 (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
반응형
'code' 카테고리의 다른 글
jquery를 사용하여 각 입력 값으로 동적으로 JSON 만들기 (0) | 2020.10.13 |
---|---|
Swift에서만 한 뷰 컨트롤러의 방향을 세로 모드로 고정하는 방법 (0) | 2020.10.13 |
GHC Haskell 확장 프로그램을 사용해야합니까? (0) | 2020.10.12 |
MS SQL 비교 날짜? (0) | 2020.10.12 |
내 bash 프롬프트가 업데이트되지 않는 이유는 무엇입니까? (0) | 2020.10.12 |