C# 如何通过单击按钮在WPF中动态更改窗口背景图像?

C# 如何通过单击按钮在WPF中动态更改窗口背景图像?,c#,wpf,xaml,C#,Wpf,Xaml,我想改变或“滚动”不同的背景图像为主窗口的基础上,一个按钮点击。不同背景的数量将是动态的,并且将基于特定文件夹中图像的数量。因此,每次程序加载时,可能会有不同数量的背景进行滚动 我还希望能够回到之前的背景图像,所以整个过程就像一个旋转木马。示例:程序加载,A.jpg作为背景图像加载。我单击“Right”按钮,A.jpg向左滑动,然后B.jpg从右侧滑入,成为新的背景图像。我再次点击“右键”,C.jpg从右边滑入。然后单击“左”,B.jpg从左侧滑回,等等 希望这是有道理的。我对XAML和WPF还

我想改变或“滚动”不同的背景图像为主窗口的基础上,一个按钮点击。不同背景的数量将是动态的,并且将基于特定文件夹中图像的数量。因此,每次程序加载时,可能会有不同数量的背景进行滚动

我还希望能够回到之前的背景图像,所以整个过程就像一个旋转木马。示例:程序加载,A.jpg作为背景图像加载。我单击“Right”按钮,A.jpg向左滑动,然后B.jpg从右侧滑入,成为新的背景图像。我再次点击“右键”,C.jpg从右边滑入。然后单击“左”,B.jpg从左侧滑回,等等


希望这是有道理的。我对XAML和WPF还很陌生,所以我只是想弄清楚我将如何做这件事。任何帮助或指导都将不胜感激。谢谢

我会在ViewModel中使用
列表视图
可观察集合
ObservableCollection
包含图像路径的动态列表。确保图像的生成操作设置为Resource。然后在Window的Background属性中放置一个ImageBrush,将源属性绑定到ListView的SelectedItem属性。图像的路径字符串遵循您可以在此处找到的方案:

根据需要(图像将被复制到资源,如果更新,则复制):

MainWindow.xaml

<Window x:Class="WinTest.MainWindow"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WinTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:TestViewModel x:Key="viewModel"/>
        <local:ImageConverter x:Key="converter"/>
    </Window.Resources>
    <Window.DataContext>
        <Binding Source="{StaticResource viewModel}" IsAsync="True"/>
    </Window.DataContext>
    <Window.Background>
        <ImageBrush ImageSource="{Binding SelectedImagePath, Converter={StaticResource converter}}"/>
    </Window.Background>
    <Grid Background="Transparent">
        <ListView Background="Transparent" SelectedValue="{Binding SelectedImagePath, Mode=TwoWay}" ItemsSource="{Binding PathList}"/>
    </Grid>
</Window>

仅此而已。

我将在ViewModel中使用
列表视图
可观察集合
ObservableCollection
包含图像路径的动态列表。确保图像的生成操作设置为Resource。然后在Window的Background属性中放置一个ImageBrush,将源属性绑定到ListView的SelectedItem属性。图像的路径字符串遵循您可以在此处找到的方案:

根据需要(图像将被复制到资源,如果更新,则复制):

MainWindow.xaml

<Window x:Class="WinTest.MainWindow"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WinTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:TestViewModel x:Key="viewModel"/>
        <local:ImageConverter x:Key="converter"/>
    </Window.Resources>
    <Window.DataContext>
        <Binding Source="{StaticResource viewModel}" IsAsync="True"/>
    </Window.DataContext>
    <Window.Background>
        <ImageBrush ImageSource="{Binding SelectedImagePath, Converter={StaticResource converter}}"/>
    </Window.Background>
    <Grid Background="Transparent">
        <ListView Background="Transparent" SelectedValue="{Binding SelectedImagePath, Mode=TwoWay}" ItemsSource="{Binding PathList}"/>
    </Grid>
</Window>

就这些。

这就是你能做到的!我已经测试过了。可以为旋转木马类型效果应用动画

public MainWindow()
{
    InitializeComponent();

    myImagesList = new List<ImageBrush>();
    ImageBrush myBrush1 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\StackOverFlowSolutions\Images\Capture.JPG")));
    ImageBrush myBrush2 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Apps-Dialog-Close-icon.png")));
    ImageBrush myBrush3 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG")));
    ImageBrush myBrush4 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG")));
    ImageBrush myBrush5 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG")));    

    myImagesList.Add(myBrush1);
    myImagesList.Add(myBrush2);
    myImagesList.Add(myBrush3);
    myImagesList.Add(myBrush4);
    myImagesList.Add(myBrush5);


    MainWin.Background = myImagesList[index];
}
private int index = 0;
private List<ImageBrush> myImagesList;

private void NextBtn_Click(object sender, RoutedEventArgs e)
{
    index++;
    MainWin.Background = myImagesList[index];
}

private void PrevBtn_Click(object sender, RoutedEventArgs e)
{
    index--;
    MainWin.Background = myImagesList[index];
}  
public主窗口()
{
初始化组件();
myImagesList=新列表();
ImageBrush myBrush1=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\StackOverFlowSolutions\Images\Capture.JPG”);
ImageBrush myBrush2=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Apps Dialog Close icon.png”);
ImageBrush myBrush3=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG”);
ImageBrush myBrush4=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG”);
ImageBrush myBrush5=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG”);
myImagesList.Add(myBrush1);
myImagesList.Add(myBrush2);
myImagesList.Add(myBrush3);
myImagesList.Add(myBrush4);
myImagesList.Add(myBrush5);
MainWin.Background=myImagesList[index];
}
私有整数指数=0;
私有列表myImagesList;
私有void NextBtn\u单击(对象发送方,路由目标)
{
索引++;
MainWin.Background=myImagesList[index];
}
私有void PrevBtn_单击(对象发送方,路由目标)
{
索引--;
MainWin.Background=myImagesList[index];
}  
XAML

<Window x:Class="StackOverFlowSolutions.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="MainWin"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="NextBtn" Width="30" Height="20" Click="NextBtn_Click">Next</Button>
        <Button Name="PrevBtn" Width="30" Height="20" Margin="297,111,176,180" Click="PrevBtn_Click">Prev</Button>
    </Grid>
</Window>

下一个
上

这就是你可以做到的!我已经测试过了。可以为旋转木马类型效果应用动画

public MainWindow()
{
    InitializeComponent();

    myImagesList = new List<ImageBrush>();
    ImageBrush myBrush1 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\StackOverFlowSolutions\Images\Capture.JPG")));
    ImageBrush myBrush2 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Apps-Dialog-Close-icon.png")));
    ImageBrush myBrush3 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG")));
    ImageBrush myBrush4 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG")));
    ImageBrush myBrush5 = new ImageBrush(new BitmapImage(new Uri(@"C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG")));    

    myImagesList.Add(myBrush1);
    myImagesList.Add(myBrush2);
    myImagesList.Add(myBrush3);
    myImagesList.Add(myBrush4);
    myImagesList.Add(myBrush5);


    MainWin.Background = myImagesList[index];
}
private int index = 0;
private List<ImageBrush> myImagesList;

private void NextBtn_Click(object sender, RoutedEventArgs e)
{
    index++;
    MainWin.Background = myImagesList[index];
}

private void PrevBtn_Click(object sender, RoutedEventArgs e)
{
    index--;
    MainWin.Background = myImagesList[index];
}  
public主窗口()
{
初始化组件();
myImagesList=新列表();
ImageBrush myBrush1=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\StackOverFlowSolutions\Images\Capture.JPG”);
ImageBrush myBrush2=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Apps Dialog Close icon.png”);
ImageBrush myBrush3=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG”);
ImageBrush myBrush4=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG”);
ImageBrush myBrush5=新的ImageBrush(新的位图图像(新的Uri(@“C:\Users\Abdul Rehman\\Desktop\1-Rao Hammas Folder\MY PROJECTS\StackOverFlowSolutions\\StackOverFlowSolutions\Images\\Capture.JPG”);
myImagesList.Add(myBrush1);
myImagesList.Add(myBrush2);
myImagesList.Add(myBrush3);
myImagesList.Add(myBrush4);
myImagesList.Add(myBrush5);
MainWin.Background=myImagesList[index];
}
私有整数指数=0;
私有列表myImagesList;
私有void NextBtn\u单击(对象发送方,路由目标)
{
索引++;
MainWin.Background=myImagesList[index];
}
私有void PrevBtn_单击(对象发送方,路由目标)
{
索引--;
MainWin.Background=myImagesList[index];
}  
XAML

<Window x:Class="StackOverFlowSolutions.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="MainWin"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="NextBtn" Width="30" Height="20" Click="NextBtn_Click">Next</Button>
        <Button Name="PrevBtn" Width="30" Height="20" Margin="297,111,176,180" Click="PrevBtn_Click">Prev</Button>
    </Grid>
</Window>

下一个
上

可能创建背景图像的
集合ViewSource
,将背景绑定到当前项目,使用