-1

我想遍历我的设置(两个设置文件,设置和监视器)并在 ItemsControl 中显示它们。问题是:我只能绑定 DefaultValue(引用绑定到 ItemsControl 的值)而不是实际值。

<UserControl x:Class="ProjectPerformance.Views.Settings"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:my="clr-namespace:ProjectPerformance.Views"
         xmlns:local="clr-namespace:ProjectPerformance"
         xmlns:properties="clr-namespace:ProjectPerformance.Properties"
         xmlns:System="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         
         d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
    <DataTemplate DataType="{x:Type sys:String}" x:Key="settingstringtemplate">
        <Grid Margin="0,2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Label Content="{Binding Name}"/>
            <TextBox Text="{Binding DefaultValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" />
        </Grid>
    </DataTemplate>
    <DataTemplate DataType="{x:Type sys:Int32}" x:Key="settinginttemplate">
        <Grid Margin="0,2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Name}" Background="Red"/>
            <TextBox Text="{Binding DefaultValue}" Grid.Column="1"/>
        </Grid>
    </DataTemplate>
    <DataTemplate DataType="{x:Type sys:Boolean}" x:Key="settingbooleantemplate">
        <Grid Margin="0,2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Name}"/>
            <CheckBox IsChecked="{Binding DefaultValue, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" />
        </Grid>
    </DataTemplate>
    <local:Settingstemplateselector x:Key="settingstemplateselector"/>
</UserControl.Resources>
<Grid TextElement.FontSize="20">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20"/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition Width="20"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
        <RowDefinition Height="20"/>
    </Grid.RowDefinitions>
    <Label Content="Settings" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" FontSize="40" VerticalAlignment="Top"/>
    <ScrollViewer Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto">
        <StackPanel>
            <Expander FontSize="15">
                <Expander.Header>
                    <TextBlock FontSize="20">General</TextBlock>
                </Expander.Header>
                <ItemsControl x:Name="lbGeneral" ItemTemplateSelector="{StaticResource settingstemplateselector}" ItemsSource="{Binding Source={x:Static properties:Settings.Default}, Path=Properties}"/>
            </Expander>
            <Expander FontSize="15">
                <Expander.Header>
                    <TextBlock FontSize="20">Monitor</TextBlock>
                </Expander.Header>
                <ItemsControl x:Name="lbMonitor" ItemTemplateSelector="{StaticResource settingstemplateselector}" ItemsSource="{Binding Source={x:Static properties:Monitor.Default}, Path=Properties}"/>
            </Expander>
            <Button x:Name="btnSave" Width="150" Margin="5" HorizontalAlignment="Right" Content="Save" FontSize="15" PreviewMouseLeftButtonDown="btnSave_PreviewMouseLeftButtonDown"/>
            <Button x:Name="btnDebug" Width="150" Margin="5" HorizontalAlignment="Right" Content="{Binding Source={x:Static properties:Monitor.Default}, Path=graph_size}" FontSize="15" PreviewMouseLeftButtonDown="btnDebug_PreviewMouseLeftButtonDown"/>
            <Button x:Name="btnRestart" Width="150" Margin="5" HorizontalAlignment="Right" Content="Restart App" FontSize="15" PreviewMouseLeftButtonDown="btnRestart_PreviewMouseLeftButtonDown"/>
        </StackPanel>
    </ScrollViewer>
</Grid>

我认为它可以像这样工作:

<TextBox Text="{Binding Source={x:Static properties:Monitor.Default}, Path={Binding Name}}"/>

但我不知道如何在 Binding 中绑定值。

4

1 回答 1

0

您可以绑定到PropertyValues属性以获取当前SettingsPropertyValue值,例如:

<ItemsControl ItemsSource="{Binding Source={x:Static properties:Monitor.Default}, Path=PropertyValues}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}" />
                <TextBlock Grid.Column="1" Text="{Binding Property.DefaultValue}" />
                <TextBlock Grid.Column="2" Text="{Binding PropertyValue}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

请注意,要使PropertyValues属性实际返回任何值,您显然需要先设置属性或检索每个属性。您可以使用一些反射动态地执行此操作:

foreach (System.Configuration.SettingsProperty property in Monitor.Default.Properties)
    typeof(Monitor).GetProperty(property.Name).GetValue(Monitor.Default);
于 2021-12-20T15:26:10.220 回答