是否可以将按钮内容数据绑定交换为代码中的其他数据绑定? [英] Can I swap a buttons content databind to a different databind in code?

查看:71
本文介绍了是否可以将按钮内容数据绑定交换为代码中的其他数据绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何示例可以使我理解如何以及是否可以在单击按钮的情况下更改c#中的数据绑定(在我的情况下是拨动开关),基本上我的应用程序中有32个按钮,而这32个按钮相同,但需要插入不同的文本,具体取决于某些拨动开关,它们当前已进行数据绑定,因此可以保存文本并从本地存储中检索文本,但是获取的值取决于这些拨动开关的状态。

I cannot find any examples to make me understand how and if I can change the databind in c# at the click of a button on, in my case a toggleswitch, Basically I have 32 buttons in my app and those 32 buttons act the same but need different text with-in them depending on some toggle switches they are currently databinded so the text can be saved and retrieved from local storage but what values it gets depends on the state of these toggle switches.

所以我目前有:

<Button x:Name="_ovButton1" Content="{Binding Source={StaticResource AppSettings}, Path=ovName1_1Value, Mode=TwoWay}" Margin="2,0,250,0" VerticalAlignment="Top" FontSize="14" Height="72" FontWeight="Bold" MouseLeftButtonUp="_ovButton1_MouseLeftButtonUp" MouseLeftButtonDown="_ovButton1_MouseLeftButtonDown" ClickMode="Hover" Hold="_ovButton1_Hold"/>

我想要当用户更改切换开关的状态以更改

and I want when a user changes the state of a toggleswitch to change the

{StaticResource AppSettings}, Path=ovName1_1Value, Mode=TwoWay}

例如:

{StaticResource AppSettings}, Path=ovName1_2Value, Mode=TwoWay}

但我找不到任何示例显示如何在c#
中执行此操作我需要这样做吗?

but I cannot find any example that shows how to do that in c# what code do I need to do that?

推荐答案

我认为您最好的选择是使用字符串集合并绑定到该集合。您可以在切换开关时更改集合,或保留6个集合并绑定到该开关的集合。

I think your best bet is going to be to use a collection of strings and bind to that collection. You can either change the collection when a toggle is switched, or keep 6 collections and bind to the collection that is for the toggle.

Xaml:

<ItemsControl x:Name="Buttons" ItemsSource="{Binding ButtonTextCollection}">
    <ItemsControl.ItemsPanel>
        <toolkit:WrapPanel/>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Width="100" Height="70" Content="{Binding}" Click="OnButtonClick"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

您的隐藏代码将为您的按钮单击提供事件处理程序

Your code-behind would have the event handler for your button click

private void OnButtonClick(object sender, RoutedEventArgs e)
{
    var text = ((Button) sender).Content.ToString();
    // Send the text
}

您的ViewModel将保存<$

Your ViewModel would hold the ButtonTextCollection property and would change based on the toggle.

public ICollection<string> ButtonTextCollection
{
    get { return _buttonTextCollection; }
    set
    {
        _buttonTextCollection = value;
        OnPropertyChanged("ButtonTextCollection");
    }
}

如果要更改文本,则需要更改 ButtonTextCollection

When you want to change the text, you would change the ButtonTextCollection

public void ChangeButtonText()
{
    ButtonTextCollection = new Collection<string> {"A", "B",...};
}

这篇关于是否可以将按钮内容数据绑定交换为代码中的其他数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆