我想将TextBox Text绑定到静态属性,如https://stackoverflow.com/a/27880765,但无法使其工作。 IntelliSense也没有任何帮助。
public static class Configuration
{
public static string Host { get; set; } = "127.0.0.1";
}
<Window x:Class="MyApp.OptionsDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyApp"
mc:Ignorable="d"
Title="OptionsDialog" Height="104.563" Width="226.845">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Host: " Grid.Column="0" Grid.Row="0"/>
<TextBox Text="{Binding Source=x:Static local:Configuration.Host}" Grid.Column="1" Grid.Row="0" Margin="1"/>
</Grid>
</Window>
我试过的东西:
{Binding Path={x:Static local:Configuration.Host}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}
- Getter调用,但InvalidCastExcpetion:System.String&lt; =&gt; System.Windows.PropertyPath {Binding Source={x:Static local:Configuration.Host}, Mode=TwoWay}
- Getter调用,InvalidOperationExcpetion:双向绑定需要路径或XPath {Binding Source={x:Static local:Configuration.Host}, Mode=TwoWay, Path=.}
Getter正确调用并显示,但没有Setter {Binding {x:Static local:Configuration.Host}, Mode=TwoWay}
- Getter被叫但没有显示,Setter没有工作`编辑:为什么它不重复!
Binding to static property是关于非静态类中的静态属性。我在静态类中询问静态属性。