我想知道是否有辦法將一個元素的屬性綁定到另一個元素,但修改其中的數據。例如,我可以將文本塊的FontSize綁定到窗口的width/20或類似的東西嗎?我已經遇到過幾次現在可能有用的區域,但總是找到解決方法(通常涉及向我的viewModel添加字段)。完全的xaml解決方案是首選。在xaml中更改綁定值
0
A
回答
1
是的,通過實施IValueConverter。
您的方案會是這個樣子的轉換器:
[ValueConversion(typeof(double), typeof(double))]
public class DivideBy20Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var f = (double) value;
return f/20.0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var f = (double)value;
return f * 20.0;
}
}
...而像這樣的XAML:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication3="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525"
x:Name="Window">
<Window.Resources>
<wpfApplication3:DivideBy20Converter x:Key="converter"></wpfApplication3:DivideBy20Converter>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox FontSize="{Binding ElementName=Window, Path=Width, Converter={StaticResource converter}}"></TextBox>
</Grid>
</Window>
+0
它很有趣,在問了這個問題後幾個小時,我的老闆在我們的培訓中覆蓋了它會話。感謝您的迴應,我一定會利用這些。 – dragoncmd 2013-02-28 19:21:14
0
您可以使用IValueConverters
來處理這樣的邏輯。
這裏是你所提到的情形的例子,可以綁定到的窗口寬度,並使用Converte
ř通過在ConverterParameter
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && parameter != null)
{
double divisor = 0.0;
double _val = 0.0;
if (double.TryParse(value.ToString(), out _val) && double.TryParse(parameter.ToString(), out divisor))
{
return _val/divisor;
}
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
XAML中提供的值來劃分寬度:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:WpfApplication7"
Title="MainWindow" Height="124" Width="464" Name="YourWindow" >
<Window.Resources>
<converters:MyConverter x:Key="MyConverter" />
</Window.Resources>
<StackPanel>
<TextBlock FontSize="{Binding ElementName=YourWindow, Path=ActualWidth, Converter={StaticResource MyConverter}, ConverterParameter=20}" />
</StackPanel>
</Window>
相關問題
- 1. 更改XAML中的綁定值
- 2. XAML在綁定中綁定
- 3. XAML - 更改圖標依賴於綁定
- 4. 在XAML中綁定到自動值
- 5. 如何更改我的代碼隱藏綁定到XAML綁定
- 6. 在KnockoutJS中使用自定義綁定更改綁定值
- 7. UWP綁定:使用C#在XAML中更改背景
- 8. 更改綁定值,不綁定本身
- 9. XAML數據綁定在屬性更改時不更新UI
- 10. XAML綁定UI更新
- 11. 在XAML中綁定XAML用戶控件
- 12. 在XAML ResourceDictionary中綁定
- 13. 在XAML中綁定資源
- 14. 綁定到在XAML
- 15. 綁定XAML在WPF
- 16. XAML中的強制綁定更新
- 17. 根據UserInterface中的值更改綁定
- 18. Datagridview在綁定時更改值
- 19. UWP/XAML如何更改數據綁定中的單個項目
- 20. 將綁定源更改回查看XAML中容器的模型
- 21. 爲什麼不更改綁定XAML中的數據?
- 22. 更改默認綁定/文本框的StringFormat在XAML
- 23. Windows 8 XAML數據綁定更新文本更改
- 24. 在xaml中更改焦點
- 25. 綁定到XAML
- 26. WPF XAML綁定
- 27. XAML綁定組
- 28. WPF XAML綁定
- 29. 如果綁定值等於NULL更改綁定值
- 30. SAPUI5數據綁定獲取更改事件中更改的值
使用'IValueConverter'概念:http://blogs.msdn.com/b/bencon/archive/2006/05/10/594886.aspx – MarcinJuraszek 2013-02-27 21:22:41