SelectedItem未更新

时间:2016-11-11 15:17:21

标签: c# xaml uwp-xaml

我有一个带有列表框的UWP应用程序,该列表框绑定到枚举列表以通过单选按钮显示不同的选项。

我想要的是,当我选择一个项目(例如每年)时,在SelectedCategory上,BudgetPeriod会更改为每年的枚举。我尝试的是以下内容:

我的列表框:

<ListBox Margin="0,0,0,12"
                         ItemsSource="{Binding BudgetPeriods}"
                         SelectedItem="{Binding SelectedCategory.BudgetPeriod}">
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListBoxItem">
                                        <RadioButton Content="{TemplateBinding Content}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                </ListBox>

Selected Category Property:
 public CategoryViewModel SelectedCategory
        {
            get { return selectedCategory; }
            set
            {
                selectedCategory = value;
                RaisePropertyChanged();
            }
        }

枚举列表:

public List<BudgetPeriod> BudgetPeriods => new List<BudgetPeriod>
    {
        BudgetPeriod.Monthly,
        BudgetPeriod.Yearly
    };

不幸的是,SelectedCategory.BudgetPeriod中的Propety始终是每月(第一个选择),并且在我选择其他内容时不会更改。

该项目是开源的,因此您可以查看整个代码:https://github.com/NPadrutt/MoneyFox.Windows/tree/budget

相关课程的链接: 查看型号: https://github.com/NPadrutt/MoneyFox.Windows/blob/budget/Src/MoneyFox.Business/ViewModels/ModifyCategoryViewModel.cs

型号: https://github.com/NPadrutt/MoneyFox.Windows/blob/budget/Src/MoneyFox.Foundation/DataModels/CategoryViewModel.cs

查看: https://github.com/NPadrutt/MoneyFox.Windows/blob/budget/Src/MoneyFox.Windows/Views/ModifyCategoryView.xaml

查看(代码背后): https://github.com/NPadrutt/MoneyFox.Windows/blob/budget/Src/MoneyFox.Windows/Views/ModifyCategoryView.xaml.cs

Android布局: https://github.com/NPadrutt/MoneyFox.Windows/blob/budget/Src/MoneyFox.Droid/Resources/layout/activity_modify_category.axml

1 个答案:

答案 0 :(得分:1)

您应该在&#34; TwoWay&#34;中设置绑定。模式:

&#13;
&#13;
private void btn_getLocation_Click(object sender, EventArgs e)
{
    try
    {
        tb_location.Clear();
        if (cb_database.Text != null)
        {
            updateConString();
            _sql.runSqlCmd(_sqlScripts.getLocation, _sqlScripts.dsRequired);
            foreach (DataTable table in _sql.ds.Tables)
            {
                foreach (DataRow dr in table.Rows)
                {
                    tb_location.Text = dr[1].ToString();
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString(), ex.Message.ToString());
    }
}
&#13;
&#13;
&#13;