在我的 MainPage 的 .NET MAUI 代碼隱藏檔案中,我想多重系結到我的 ViewModel 的元素以及 XAML 中定義的網格的寬度和高度。
目的是讓每個網格單元格中的字體自動調整為單元格大小,并且還尊重“文本小”布林值。
問題似乎是,與“ScreenGrid.Height”的系結沒有正確解決
(MultiValueConverter 只接收一個用空值填充的陣列)。
ScreenGrid 在 Xaml 中定義。
僅解決 3 個系結中的第一個按預期作業。
將 label 的 BindingContext 設定為 ScreenGrid 不起作用。
是否有我不知道的明顯錯誤或規則?
XAML:
<ContentPage [...]
xmlns:viewmodels="clr-namespace:Renderer.ViewModels"
x:Class="Renderer.MainPage"
x:DataType="viewmodels:CduViewModel">
<AbsoluteLayout>
<!-- Screen -->
<Grid x:Name="ScreenGrid"
RowDefinitions="*, *, *, *, "
ColumnDefinitions="*,*,*,*,*,*,*,*,"
[...]>
</Grid>
</AbsoluteLayout>
</ContentPage>
public partial class MainPage : ContentPage
{
public MainPage(ICduViewModel viewModel)
{
InitializeComponent();
this.ViewModel = viewModel;
AddScreenCells(); //Set up "Screen"
}
public void AddScreenCells()
{
var frameConverter = new Converters.FrameToFontSizeConverter();
for (int row = 0; row < ScreenGrid.RowDefinitions.Count; row )
{
for (int col = 0; col < ScreenGrid.ColumnDefinitions.Count; col )
{
//Label
Label label = new()
{
FontAutoScalingEnabled = false
};
label.SetBinding(Label.FontSizeProperty, new MultiBinding
{
Bindings = new Collection<BindingBase>
{
//This binding works, if used seperately
new Binding($"{nameof(CduViewModel.Screen)}[{row}].{nameof(CduText.Sizes)}[{col}]"),
//These bindings only work when setting the BindingContext to ScreenGrid, which kills the binding above.
new Binding($"{nameof(ScreenGrid)}.{nameof(ScreenGrid.Height)}"),
new Binding($"{nameof(ScreenGrid)}.{nameof(ScreenGrid.Width)}"),
},
Converter = frameConverter
});
ScreenGrid.Add(label, col, row);
}
}
}
}
}
uj5u.com熱心網友回復:
設定每個 ScreenGrid Binding 的“source:”引數:
new Binding($"{nameof(ScreenGrid)}.{nameof(ScreenGrid.Height)}",
source: ScreenGrid)
這使得ScreenGrid該 Binding 的 BindingContext。
我沒有測驗;可能{nameof(ScreenGrid)}.從路徑引數中洗掉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/521421.html
標籤:C#xml捆绑毛伊岛
