Automatically represent binding object by correct

2019-09-17 08:27发布

This question already has an answer here:

working myself into WPF and MVVM, I'm trying to do as much dynamical stuff as possible (and feasible).

For example, I'm now trying to create a Settings Page, which lists all Settings I have in my Properties.Settings.Default by binding to a property in its ViewModel.

Now that works just fine as long as there are only Strings but I'd like to have other settings (e.g. numeric or Image) represented by the corresponding WPF Control (NumericUpDown and Image respectively).

I know that this is probably not possible and its just me getting overly excited about the features of WPF but then I thought.. actually why wouldn't it be possible?

EDIT: Regarding the comments, I actually haven't any attempts to achieve this behaviour, however I have a dynamic listing of properties here:

<TabControl Style="{StaticResource MyMetroTabControl}" Background="{DynamicResource AccentColorBrush}" TabStripPlacement="Left" MinWidth="{Binding RelativeSource={RelativeSource AncestorType=Controls:MetroWindow}, Path=SettingsTabControlMinWidth}" x:Name="SettingsTabControl" Grid.Column="1">
<TabItem Style="{StaticResource MyMetroTabItem}" Background="{DynamicResource AccentColorBrush}"  Header="Application Paths" MinWidth="200">
    <ItemsControl ItemsSource="{Binding Path=SettingsDictionary}" Grid.IsSharedSizeScope="True">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="SettingNameColumnWidth"/>
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="{Binding Path=Key}" FontSize="15" VerticalAlignment="Center" Grid.Column="0"/>
                    <TextBox Text="{Binding Path=Value}" Grid.Column="1" Margin="5"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</TabItem>
<TabItem Header="Test" Style="{StaticResource MyMetroTabItem}" Background="{DynamicResource AccentColorBrush}">

</TabItem>

Where SettingsDictionary basically is just a property generated Dictionary with the Settings Property Name as Dictionary Key and the Settings Value as Dictionary Value. Actually I'm not quite happy with this solution yet, as the Property Names aren't correctly formatted (missing spaces etc)

Regards, Xaser

0条回答
登录 后发表回答