我想遍历我的设置(两个设置文件,设置和监视器)并在 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 中绑定值。