klutch1991
klutch1991

Reputation: 239

Ways to create reusable wpf elements?

I'm definitely new to wpf, and I wonder, what could I do with the following situation (for example):
I have a xaml markup file, and somewhere in that file I declare two <Border> elements, and each of them has nested elements:

<Border Grid.Row="1">
    <StackPanel>
        <TextBox Text="1st el"/>
        <ComboBox ItemsSource="{Binding }">
    </StackPanel>
</Border>

<Border Grid.Row="2">
    <StackPanel>
        <TextBox Text="2nd el"/>
        <ComboBox ItemsSource="{Binding SomeVMProp}">
    </StackPanel>
</Border>

Everything works fine, but this code seems to break DRY principle. So here're my questions:

PS Sorry if my question is incorrect, but I'm expecting somebody to give me an idea about dealing with situations like that.

Upvotes: 2

Views: 1992

Answers (2)

Exxoff
Exxoff

Reputation: 181

Right-click your project, click New -> UserControl. Name it to whatever you want. Paste your XAML where you want it.

Use it in you MainWindow.xaml (or whatever your main window is called) by referencing <local:MyUserControl/>.

Upvotes: 7

Se&#231;kin
Se&#231;kin

Reputation: 2826

You should develop custom control for this situation. You can have a custom control library. Take a look this msdn link

Upvotes: -1

Related Questions