我已經創建了一個組合框和im綁定在MVVM模式,但我的屬性不綁定到視圖,我與itemsource
和selectvalue
混淆。 我可以對view.xaml做什麼改變?我猜我的模型和視圖模型中的其餘代碼是完美的。WPF MVVM Combobox綁定
這是我的模型
namespace screensaver.Models {
class ConfigurationModel {
public int _resolution;
private ObservableCollection <ConfigurationModel> Resolution {
get {
return Resolution;
}
set {
Resolution = value;
}
}
public ConfigurationModel() {
Resolution = new ObservableCollection <ConfigurationModel>() {
new ConfigurationModel() {
_resolution = 360 * 720
},
new ConfigurationModel() {
_resolution = 720 * 1080
},
new ConfigurationModel() {
_resolution = 1080 * 2060
}
};
}
}
}
這是我的ViewModel
namespace screensaver.ViewModels {
class ConfigurationViewModel {
private ObservableCollection <ConfigurationModel> _resolution;
public ObservableCollection <ConfigurationModel> Resolution {
get {
return Resolution;
}
set {
Resolution = value;
}
}
}
}
這是我的看法XAML代碼
<Window x:Class="screensaver.Views.ConfigurationWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:screensaver.ViewModels" Title="ConfigurationWindow"
Height="1000" Width="500">
<Grid>
<Label Content="Display" HorizontalAlignment="Left" Margin="7,12,0,0" VerticalAlignment="Top" />
<ComboBox HorizontalAlignment="Left" Margin="322,14,0,0" VerticalAlignment="Top" Width="120" />
<ComboBox ItemsSource="{Binding Resolution}" SelectedItem="{Binding
Resolution, Mode=TwoWay}" DisplayMemberPath="{Binding Resolution}" HorizontalAlignment="Left" Margin="74,13,0,0" VerticalAlignment="Top" Width="120" />
<Label Content="Resolution" HorizontalAlignment="Left" Margin="250,13,0,0" VerticalAlignment="Top" />
<Button Content="Save" HorizontalAlignment="Left" Margin="80,362,0,0" VerticalAlignment="Top" Width="75" />
<Button Content="Close" HorizontalAlignment="Left" Margin="350,360,0,0" VerticalAlignment="Top" Width="75" />
<Label Content="Height" HorizontalAlignment="Left" Margin="72,178,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="140,181,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Width" HorizontalAlignment="Left" Margin="290,175,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="346,179,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Top" HorizontalAlignment="Left" Margin="76,253,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="140,255,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Left" HorizontalAlignment="Left" Margin="292,250,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="349,252,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
您可以使用像史努比的工具來檢查您在運行時綁定,看看他們爲什麼不加工。我看不到任何證據表明您已將DataContext設置在任何地方。 – Will
我做了這個編輯太多,但仍然我的綁定不起作用 – vijay