버튼의 데이터 템플릿을 WPF XAML에서 C# 코드 I.E로 변경하려고합니다. 나는 프로그래밍 방식으로 버튼을 .cs 파일로 만들고 싶다.XAML을 C# 코드로 변경
XAML은 다음과 같습니다
<ItemsControl DataContext="{Binding Source={StaticResource Things}}" ItemsSource="{Binding}" Name="buttons" Margin="0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<local:FishEyePanel ScaleToFit="false"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Button Command="Open" Tag="{Binding [email protected], Path=Value}" Margin="5" Padding="5" HorizontalContentAlignment="Center" Width="{Binding [email protected]}" Height="{Binding [email protected]}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
가 라인 :
<XmlDataProvider x:Key="Things" XPath="Things/Thing">
<x:XData>
<Things xmlns="">
<Thing Button="uno1" Tag="abc" Width="200" Height="200"/>
</Things>
</x:XData>
</XmlDataProvider>
: 나는 XML 파일에 모든 버튼을 결합 곳
<Button Command="Open" Tag="{Binding [email protected], Path=Value}" Margin="5" Padding="5" HorizontalContentAlignment="Center" Width="{Binding [email protected]}" Height="{Binding [email protected]}"/>
여기입니다 파일입니다
하지만 내가 원하는 것은 미리 정의 된 모든 바닥이있는 xml 파일을 호출하는 대신 버튼과 함께 CS로 메소드를 호출하는 것과 같습니다. 이 :
public void createButtons()
{
Button button = new Button();
button.Tag = "I am from cs";
buttons.Items.Add(button);
}
이게 가능합니까?
우리의'createButtons()'메소드에 대해 정확히 작동하지 않는 것은 무엇입니까? 일반적으로 XAML에서 가능한 모든 것은 C# 코드에서도 가능합니다. –
왜 그렇게하고 싶습니까? XAML 사용에 대한 잘못된 점은 무엇입니까? –
예, 가능합니다. 너 뭐 해봤 니? 끊어 버린 코딩 포인트가 있다면 그것에 대해 물어보십시오. 어딘가 시작하고 싶다면,'buttons.ItemContainerStyle'과'buttons.ItemsPanel'을보고, 그것들의 타입을보고 거기에서부터 시작하십시오. 왜 당신이 처음에 이것을하고 싶은지에 대해 나에게 너무 많은 의미를주지 않습니다. 왜 그렇게하고 싶은지 설명하면 누군가가 더 나은 접근 방식을 제안 할 수 있습니다. –