0
我遇到過這種情況,我需要根據選擇哪個切換按鈕來動態設置組合框的Items源。如何根據bool值動態設置ComboBox綁定?
在應用程序中有兩個切換按鈕,高/普通。如果高位切換,我想將組合框的綁定設置爲higherGradePointKV
,否則如果普通切換爲ordinaryGradePointKV
。
到目前爲止,我設置了Bool屬性,用於更高/普通以及在視圖模型的構造函數中調用的。
我正在努力的部分是在此方法中設置來自代碼的綁定。
有誰知道我可以如何設置這個實現的其餘部分?
這就是布爾屬性和列表被定義並稱爲視圖模型:
namespace LC_Points.ViewModel
{
public class MainViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
//call methods to initilise list data
SetGradeComboBoxBinding();
GetOrdinaryGradePairs();
GetHigherGradePairs();
}
public List<StringKeyValue> higherGradePointKV { get; set; }
public List<StringKeyValue> ordinaryGradePointKV { get; set; }
//ordinary toggle button bool
private bool _isOrdinaryToggled;
public bool IsOrdinaryToggled
{
get
{
return _isOrdinaryToggled;
}
set
{
_isOrdinaryToggled = value;
RaisePropertyChanged("IsOrdinaryToggled");
}
}
//Higher toggle button bool property
private bool _isHigherToggled;
public bool IsHigherToggled
{
get
{
return _isHigherToggled;
}
set
{
_isHigherToggled = value;
RaisePropertyChanged("IsHigherToggled");
}
}
//Sets the grade combo box binding based on button toggled.
public void SetGradeComboBoxBinding()
{
if(_isHigherToggled)
{
//set binding to higherGradePointKV
}
else if(_isOrdinaryToggled)
{
//set binding to higherGradePointKV
}
}
public class StringKeyValue
{
public string Key { get; set; }
public int Value { get; set; }
}
public void GetOrdinaryGradePairs()
{
List<StringKeyValue> ordianryGradePointKVTemp = new List<StringKeyValue>();
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "A1", Value = 60 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "A2", Value = 50 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "B1", Value = 45 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "B2", Value = 40 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "B3", Value = 35 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "C1", Value = 30 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "C2", Value = 25 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "C3", Value = 20 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "D1", Value = 15 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "D2", Value = 10 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "D3", Value = 5 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "E,F,NG", Value = 0 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "Pass", Value = 30 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "Merit", Value = 50 });
ordianryGradePointKVTemp.Add(new StringKeyValue { Key = "Distinction", Value = 70 });
ordinaryGradePointKV = ordianryGradePointKVTemp;
}
public void GetHigherGradePairs()
{
List<StringKeyValue> higherGradePointKVTemp = new List<StringKeyValue>();
higherGradePointKVTemp.Add(new StringKeyValue { Key = "A1", Value = 100 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "A2", Value = 90 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "B1", Value = 85 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "B2", Value = 80 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "B3", Value = 75 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "C1", Value = 70 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "C2", Value = 65 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "C3", Value = 60 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "D1", Value = 55 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "D2", Value = 50 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "D3", Value = 45 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "E,F,NG", Value = 0 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "Pass", Value = 30 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "Merit", Value = 50 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "Distinction", Value = 70 });
higherGradePointKV = higherGradePointKVTemp;
}
}
}
這是目前在那裏經由XAML指定只有一個物品來源視圖:
<Page x:Class="LC_Points.MainPage"
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:local="using:LC_Points"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding Source={StaticResource Locator}}"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40*" />
<RowDefinition Height="20*" />
<RowDefinition Height="30*" />
<RowDefinition Height="30*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<!-- TitlePanel contains the name of the application and page title -->
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock Style="{StaticResource SubheaderTextBlockStyle}" Text="LC POINTS" />
<TextBlock Margin="9,-7,0,0"
Foreground="DarkGreen"
Style="{StaticResource HeaderTextBlockStyle}"
Text="Home" />
</StackPanel>
<ComboBox x:Name="gradeCmbBx"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Width="60"
HorizontalAlignment="Right"
DisplayMemberPath="Key"
ItemsSource="{Binding ordinaryGradePointKV}" />
<ToggleButton x:Name="higherTglBtn"
Grid.Row="3"
HorizontalAlignment="Left"
Content="Higher"
IsChecked="{Binding IsHigherToggled,
Mode=TwoWay}" />
<ToggleButton x:Name="ordinaryTglBtn"
Grid.Row="3"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Content="Ordinary"
IsChecked="{Binding IsOrdinaryToggled,
Mode=TwoWay}" />
</Grid>
</Page>
好的,在這種情況下,我需要設置'higherGradePointKV',如果'IsOrdinaryToggled',我是否需要設置第二個樣式?你能否顯示實現請 –
在這種情況下,只交換ItemsSource綁定,第一個是默認值,當IsHigherToggled爲true時,觸發器將覆蓋它。 – Cyral
對,我看到你在那裏做什麼,應該工作完美,我會稍後測試並報告結果。 –