C# 在代码中找不到添加到mainwindow.xaml的对象
我在一个新的C#项目的主窗口中添加了一个图像对象C# 在代码中找不到添加到mainwindow.xaml的对象,c#,visual-studio-2010,visual-studio,xaml,reference,C#,Visual Studio 2010,Visual Studio,Xaml,Reference,我在一个新的C#项目的主窗口中添加了一个图像对象 我不知道我是否做错了,我对C#还是个新手。尝试将ImageSource绑定到UI上的属性,这比在代码隐藏中引用控件要好 例如: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/w
我不知道我是否做错了,我对C#还是个新手。尝试将ImageSource绑定到UI上的属性,这比在代码隐藏中引用控件要好 例如:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="UI">
<Grid DataContext="{Binding ElementName=UI}">
<Grid.RowDefinitions>
<RowDefinition Height="150*" />
<RowDefinition Height="161*" />
</Grid.RowDefinitions>
<Image Source="{Binding MyImageSource}" Stretch="Fill" Grid.RowSpan="2" Width="320" Height="240" Margin="80,26,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
<TextBox Grid.Row="1" Height="25" HorizontalAlignment="Left" Margin="155,119,0,0" Name="kinectStatusTB" VerticalAlignment="Top" Width="111" Text="Disconnected" />
<TextBlock Grid.Row="1" Height="18" HorizontalAlignment="Left" Margin="80,122,0,0" Name="textBlock1" Text="Kinect Status" VerticalAlignment="Top" Width="69" />
</Grid>
</Window>
您是否尝试了
x:Name
而不是Name
?以及代码行放在哪里?在WPF意义上,这是错误的,在WPF UI逻辑和代码应该分开的情况下,您应该绑定到ImageSource
而不是引用代码后面的WPF控件。除此之外,您的代码应该可以正常工作。我没有将x:Name作为选项
image1.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
96, 96, //DPI
PixelFormats.Bgr32, //format
null,
pixels, //where the data is stored
stride);
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="UI">
<Grid DataContext="{Binding ElementName=UI}">
<Grid.RowDefinitions>
<RowDefinition Height="150*" />
<RowDefinition Height="161*" />
</Grid.RowDefinitions>
<Image Source="{Binding MyImageSource}" Stretch="Fill" Grid.RowSpan="2" Width="320" Height="240" Margin="80,26,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
<TextBox Grid.Row="1" Height="25" HorizontalAlignment="Left" Margin="155,119,0,0" Name="kinectStatusTB" VerticalAlignment="Top" Width="111" Text="Disconnected" />
<TextBlock Grid.Row="1" Height="18" HorizontalAlignment="Left" Margin="80,122,0,0" Name="textBlock1" Text="Kinect Status" VerticalAlignment="Top" Width="69" />
</Grid>
</Window>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private ImageSource _myImageSource;
public MainWindow()
{
InitializeComponent();
}
public ImageSource MyImageSource
{
get { return _myImageSource; }
set { _myImageSource = value; NotifyPropertyChanged("MyImageSource"); }
}
private void SetImage()
{
// Your logic
MyImageSource = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
96, 96, //DPI
PixelFormats.Bgr32, //format
null,
pixels, //where the data is stored
stride);
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}