A TwoWay or OneWayToSource binding cannot work with the read-only Value property of the WpfLocalization.Localization.KeyLocalizationListener type

<TextBox x:Name="textBox_date" IsReadOnly="True" HorizontalAlignment="Stretch" Text="{l:Localization Key=The_current_date}" VerticalAlignment="Top" Margin="0,172,0,0"/> namespace WpfLocalization.Localization { /// <summary> /// Π Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ‚ΠΊΠΈ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π»ΠΎΠΊΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½ΡƒΡŽ строку ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ ΠΈΠ»ΠΈ привязкС /// </summary> [ContentProperty(nameof(ArgumentBindings))] public class LocalizationExtension : MarkupExtension { private Collection<BindingBase> _arguments; public LocalizationExtension() { } public LocalizationExtension(string key) { Key = key; } /// <summary> /// ΠšΠ»ΡŽΡ‡ Π»ΠΎΠΊΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½ΠΎΠΉ строки /// </summary> public string Key { get; set; } /// <summary> /// ΠŸΡ€ΠΈΠ²ΡΠ·ΠΊΠ° для ΠΊΠ»ΡŽΡ‡Π° Π»ΠΎΠΊΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½ΠΎΠΉ строки /// </summary> public Binding KeyBinding { get; set; } /// <summary> /// АргумСнты Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠΉ Π»ΠΎΠΊΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ строки /// </summary> public IEnumerable<object> Arguments { get; set; } /// <summary> /// ΠŸΡ€ΠΈΠ²ΡΠ·ΠΊΠΈ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠΉ Π»ΠΎΠΊΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ строки /// </summary> public Collection<BindingBase> ArgumentBindings { get { return _arguments ?? (_arguments = new Collection<BindingBase>()); } set { _arguments = value; } } public override object ProvideValue(IServiceProvider serviceProvider) { if (Key != null && KeyBinding != null) throw new ArgumentException($"НСльзя ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ Π·Π°Π΄Π°Ρ‚ΡŒ {nameof(Key)} ΠΈ {nameof(KeyBinding)}"); if (Key == null && KeyBinding == null) throw new ArgumentException($"НСобходимо Π·Π°Π΄Π°Ρ‚ΡŒ {nameof(Key)} ΠΈΠ»ΠΈ {nameof(KeyBinding)}"); if (Arguments != null && ArgumentBindings.Any()) throw new ArgumentException($"НСльзя ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ Π·Π°Π΄Π°Ρ‚ΡŒ {nameof(Arguments)} ΠΈ {nameof(ArgumentBindings)}"); var target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); if (target.TargetObject.GetType().FullName == "System.Windows.SharedDp") return this; // Если Π·Π°Π΄Π°Π½Ρ‹ привязка ΠΊΠ»ΡŽΡ‡Π° ΠΈΠ»ΠΈ список привязок Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ², // Ρ‚ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ BindingLocalizationListener if (KeyBinding != null || ArgumentBindings.Any()) { var listener = new BindingLocalizationListener(); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ привязку для ΡΠ»ΡƒΡˆΠ°Ρ‚Π΅Π»Ρ var listenerBinding = new Binding { Source = listener }; var keyBinding = KeyBinding ?? new Binding { Source = Key }; var multiBinding = new MultiBinding { Converter = new BindingLocalizationConverter(), ConverterParameter = Arguments, Bindings = { listenerBinding, keyBinding } }; // ДобавляСм всС ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Π΅ привязки Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² foreach (var binding in ArgumentBindings) multiBinding.Bindings.Add(binding); var value = multiBinding.ProvideValue(serviceProvider); // БохраняСм Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ привязки Π² ΡΠ»ΡƒΡˆΠ°Ρ‚Π΅Π»Π΅ listener.SetBinding(value as BindingExpressionBase); return value; } // Если Π·Π°Π΄Π°Π½ ΠΊΠ»ΡŽΡ‡, Ρ‚ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ KeyLocalizationListener if (!string.IsNullOrEmpty(Key)) { var listener = new KeyLocalizationListener(Key, Arguments?.ToArray()); // Если локализация навСшана Π½Π° DependencyProperty ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° DependencyObject if ((target.TargetObject is DependencyObject && target.TargetProperty is DependencyProperty) || target.TargetObject is Setter) { var binding = new Binding(nameof(KeyLocalizationListener.Value)) { Source = listener, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }; return binding.ProvideValue(serviceProvider); } // Если локализация навСшана Π½Π° Binding, Ρ‚ΠΎ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ ΡΠ»ΡƒΡˆΠ°Ρ‚Π΅Π»Ρ var targetBinding = target.TargetObject as Binding; if (targetBinding != null && target.TargetProperty != null && target.TargetProperty.GetType().FullName == "System.Reflection.RuntimePropertyInfo") { targetBinding.Path = new PropertyPath(nameof(KeyLocalizationListener.Value)); targetBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; return listener; } // Π˜Π½Π°Ρ‡Π΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Π»ΠΎΠΊΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½ΡƒΡŽ строку return listener.Value; } return null; } } } 
  • Well, that's your {l:Localization} problem. What is inside? - VladD

1 answer 1

Got this code ? Try adding Mode = BindingMode.OneWay to the created Binding and MultiBinding.